472,124 Members | 1,397 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,124 software developers and data experts.

what is def __init__?

46
Hey guys

on my quest to learn python I keep coming across:
def __init__

I think I understand it is defining a method called "init" - but what's with all the underscores. Or is it some builtin method of Python?

Sorry - confused

Cheers!
Jun 19 '07 #1
3 25881
ilikepython
844 Expert 512MB
Hey guys

on my quest to learn python I keep coming across:
def __init__

I think I understand it is defining a method called "init" - but what's with all the underscores. Or is it some builtin method of Python?

Sorry - confused

Cheers!
__init__ is the function that is called automatically upon an object's construction:
Expand|Select|Wrap|Line Numbers
  1. class Test:
  2.     def __init__(self, num):    # called when object is constructed
  3.         self.value = num
  4.  
  5.     def getVal(self):
  6.         return self.value
  7.  
  8. obj = Test(5)       # Test.__init__ called; sets obj.value to num (5) 
  9.  
  10. print obj.getVal()   # prints "5"
  11.  
The double underscores are used when operator overloading. For example, the above class could be written like this:
Expand|Select|Wrap|Line Numbers
  1. class Test:
  2.     def __init__(self, num):    # called when object is constructed
  3.         self.value = num
  4.  
  5.     def __repr__(self):           # called when object is printed
  6.         return self.value
  7.  
  8. obj = Test(5)       # Test.__init__ called; sets obj.value to num (5) 
  9.  
  10. print obj   # Test.__repr__ called; prints "5"
  11.  
Does that help?


Edit: Just wanted to add that the double underscores in simple terms mean that the function will get called on a special occasion (operator overloading). For example, __init__ gets called when an object is created, __repr__ gets called when the object gets printed, __del__ gets called when object goes out of scope, and there are many others.

Edit 2: Another thing, __init__ is usually used to set up the object by using the arguements to make variables of the object. For example, let's say you have a rectangle class. You'd want the object to have for instance, width and heigth variables. The __init__ would look like this:
Expand|Select|Wrap|Line Numbers
  1. class Rect:
  2.     def __init__(self, width, height):
  3.         self.width = width
  4.         self.height = height
  5.  
  6. myRect = Rect(5, 7)
  7.  
Jun 19 '07 #2
ateale
46
thanks ilikepython - it makes sense - will read over that a few more times

thanks for explaining!
Jun 19 '07 #3
ilikepython
844 Expert 512MB
thanks ilikepython - it makes sense - will read over that a few more times

thanks for explaining!
You're welcome, if you have a question, feel free to ask.
Jun 19 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Jim Jewett | last post: by
7 posts views Thread by Michele Simionato | last post: by
6 posts views Thread by Steven Bethard | last post: by
14 posts views Thread by Axel Straschil | last post: by
21 posts views Thread by Sriek | last post: by
7 posts views Thread by Kent Johnson | last post: by
19 posts views Thread by dickinsm | last post: by
2 posts views Thread by Alan Isaac | last post: by
2 posts views Thread by Neil Cerutti | last post: by
25 posts views Thread by Erik Lind | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.