What are the advantages of using the __init__ function to initiate a class?
MIT 6.189 A Gentle Introduction to Programming Using Python (OCW)
Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.
Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
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?
Not the answer you are looking for? Search for more explanations.
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:
Very nice, I get it. It's just there to iniatialize the instances. thanks!
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) ....