Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

Shadowys

  • 2 years ago

What are the advantages of using the __init__ function to initiate a class?

  • This Question is Closed
  1. estudier
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 3

    Where is that in the course materials?

  2. Shadowys
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Just a question while I was tk-ing...most object-oriented programming choose to have a init function to iniatiate it.

  3. estudier
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 3

    Strictly speaking, it should be initializes a class instance, right?

  4. Shadowys
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  5. estudier
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 3

    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...:-)

  6. Shadowys
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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?

  7. estudier
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 3

    This will save me a lot of typing: http://stackoverflow.com/questions/8609153/why-do-we-use-init-in-python-classes

  8. Shadowys
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Very nice, I get it. It's just there to iniatialize the instances. thanks!

  9. estudier
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 3

    yw

  10. snark
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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) ....

  11. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Ask a Question
Find more explanations on OpenStudy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.