learning OOP - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: learning OOP (/thread-6904.html) |
learning OOP - workerbee - Dec-13-2017 I thought by thinking out loud here I could get some clarity. I have been wondering for awhile why __init__ method was necessary think I am getting it. class MyClass: def method(self): return 'instance method called', self @classmethod def classmethod(cls): return 'class method called', cls @staticmethod def staticmethod(): return 'static method called' obj = MyClass() print(obj.method())it seemed only way I could interact with object or methods inside and introduce data was like above. but with __init__ method it acts a store place for the data I introduce from outside the class into the __init__ method which i then can transfer to other methods in the class. This seems to be a be for now a big benefit of using __init__ for me. class MyClass: def __init__(self,yo,yawn ): self.yo = yo self.yawn = yawn def method(self): return 'insatance method called', self def method_one(self): return 'boss'+ self.yo + self.yawn def classmethod(cls): return 'class method called', cls def staticmethod(): return 'static method called' student = MyClass('keep','clean') print(MyClass.method_one(student))I am interested in getting terminology down for these aspects of the code so I can research more on my own. student = MyClass('keep','clean') MyClass.method_one(student) I know I am instating my object student, with MyClass but not what the () area is called where 'keep','clean' are which I would call arguments or parameters and then the re-introduction of my object into my class connected to a method within the class ->MyClass.method_one(student)..Sorry if not clear as I am not clear yet on object oriented programming. RE: learning OOP - Windspar - Dec-13-2017 __init__ is a constructor. It build objects. You only need it if you want to init object. class A: pass A.a = 10 # build variables on the fly print(A.a)example # Class as a Container class MyContainer: a = 9 b = 5 @classmethod def method(cls): return cls.a * cls.b print(MyContainer.method()) class Date: # object constructor def __init__(self, day, month, year): self.day = day self.month = month self.year = year # object method def method(self): return '{0:02}-{1:02}-{2}'.format(self.day, self.month, self.year) # class instance method @classmethod def from_string(cls, string): day, month, year = map(int, string.split('-')) return cls(day, month, year) # return object # class as a namespace @staticmethod def is_valid(string): day, month, year = map(int, string.split('-')) return day <= 31 and month <= 12 and year <= 2018 date = Date(5, 7, 2011) # Date.__init__(object, 5,7,2011) print(date.method()) date = Date.from_string('11-09-2016') print(date.method()) print(Date.is_valid('11-13-2016')) RE: learning OOP - workerbee - Dec-13-2017 Thank You..I have questions about instantiating the objects but let me think before I ask the questions. |