What are the advantages of using the __init__ function to initiate a class?
Where is that in the course materials?
Just a question while I was tk-ing...most object-oriented programming choose to have a init function to iniatiate it.
Strictly speaking, it should be initializes a class instance, right?
Yup, but I don't really see why I shouldn't call the instance straight from the class instead. Unless, of course, I am supposed to use the class to start a function. However, in python 2.x classes need to be initiated, quite annoying actually.
That is sort of one of the reasons for going over to 3 although the transition will likely take some time. Then we can have all the same arguments that we have in "grown up" languages all over again...:-)
LOL I getcha. Programming objects in python 2.x is a pain and normally I tk in Python 3.x instead. So, basically __init__ is just there to ...init a function contained in the class, then?
This will save me a lot of typing: http://stackoverflow.com/questions/8609153/why-do-we-use-init-in-python-classes
Very nice, I get it. It's just there to iniatialize the instances. thanks!
yw
you can initialize class values in the class. for example, directly initialize two values - node = Node(a,b), where __init__ can be used to set class values. Same as n = Node() # create a new Node thingy n.var1 = a # assign a value to a class variable n.var2 = b # assign another value instead you have def __init__(self,a,b,): self.var1 = a self.var2 = b it's useful if you want to initialize many objects of this class with different values.- n1 = Node(1,2) n2 = Node(3,4) ....
Join our real-time social learning platform and learn together with your friends!