+ 3

Python OOP

First I will say I loved this Python 3 tutorial, right up to the OOP module. At this point it seems to barely explain anything. At the magic-methods I am completely lost. Should I have taken a prerequisite to Python in order to understand the Python OOP module? -------------------------------------------------- Please don't answer. I gave up, due to everyone answering questions I did NOT ask, rather than answering what is written above. I am deleting the app. I won't return until after I learn python elsewhere, if at all. Somewhere. that learning is the priority, rather than who can give each other the most votes and praise for providing bad answers being the priority. Good luck with that. --------------------------------------------------

1st Feb 2018, 12:45 PM
mark
7 Answers
+ 4
I think you should take it easy on yourself and get more used to it. __init__ is the most popular magic method, if you work with others you should get your head around it. Basically they are used when the usual method cannot be used or rather not convenient to be used!
1st Feb 2018, 9:44 AM
Strange
Strange - avatar
+ 5
Init is the initializer. It is useful when instantiating classes, like so: class Human: #Class "Human" def __init__(self, name, age): #Self has to be there, but not when instantiating self.name = name self.age = age h1 = Human("John", 10) #h1 "is a" Human named John, with age 10 print(h1.name) print(h1.age) #John #10 So, when you are creating a Human from this human class, you pass parameters to the __init__ method to initialize its attributes. Hope this wasn't too confusing. Don't worry, I also found it confusing, but the trick is to play around with the code and see what you can or cannot do. 😉
1st Feb 2018, 10:06 AM
blackcat1111
blackcat1111 - avatar
+ 3
@Mark Technically, I was answering your later question regarding the usage and usefulness of the initializer, and not your original question. I agree that it did not answer your main question, but you were confused afterwards (to my understanding) about the initializer. Hence I tried to clear your doubts. If you still cannot understand the usefulness and applications of the __init__, then apparently you haven't been reading my answer. This also somewhat contradicts your previous claim of having understood the lesson, does it not? I'm not trying to blame you for anything, but your (rather) offensive tone may dissuade people from attempting to help you. If we may have misunderstood your question, then please inform us instead of shooting down our answers. I'm not one to get offended (normally) but I cannot say the same for others here.
1st Feb 2018, 11:04 AM
blackcat1111
blackcat1111 - avatar
+ 2
class Cat: def __init__(self, color, legs): self.color = color self.legs = legs felix = Cat("ginger", 4) rover = Cat("dog-colored", 4) stumpy = Cat("brown", 3) What was done is that the def __init__ is a constructor which is a function(method) that assigns default properties or behaviors to a class, in this case Cat. A class is just a blueprint for an object, Now when Felix which is an instance of the class or rather an object is created, it already has the properties of color and legs, all that is needed is to state their value. That is a good example of the magic method! I hope it helps
1st Feb 2018, 10:09 AM
Strange
Strange - avatar
+ 1
And I get my head around it where? Another course? I didn't not understand the __init__ being the most used. or where they are useful. --------------------------------- rewritten for clarity below --------------------------------- __init__ being the most common is not the question here. Being a simple statement and from a previous lesson of the question at hand lesson, it is easily understood Where they are useful is also not the question here. Also not from the lesson stated, Also from lesson before the lesson in question, implying that was understood in order to get to the lesson in the question.
1st Feb 2018, 11:40 AM
mark
0
the problem with the answers here is nobody seemed to properly read the question. Please note the lesson where lost is magic methods. the answers here are from two lessons before magic methods. Meaning I understood that in order to get to the magic methods lesson.
1st Feb 2018, 10:54 AM
mark
0
@ blackcat Please reread it and notice the double negative negate each other. I apologize for the confusion it caused you. That said, this question is so far off course now, I don't expect anyone to answer it, but rather go on and on about the classes lesson instead. thanks to the three of you. So please just forget I asked it. It was a mistake on my part, to think that I could ask for help here and get anything useful in reply. Lesson learned! Even docs.python.org search box for magic methods produces only result. the glossary, in which there is no magic methods listed. If I can not get help at the official site, and no real help is offered here. Then this course has simply failed me at 75%. I guess, it is time for a new course at a new school.
1st Feb 2018, 11:26 AM
mark