Instance method, class method and static method | Python tutorial for absolute beginners in Hindi

Veröffentlicht am: 09 März 2021
auf dem Kanal: CodeWithRajat
2,378
38

Instance method, class method and static method | Python tutorial for absolute beginners in Hindi


Instance Methods

Through the self parameter, instance methods can freely access attributes and other methods on the same object. This gives them a lot of power when it comes to modifying an object’s state.

Not only can they modify object state, instance methods can also access the class itself through the self.__class__ attribute. This means instance methods can also modify class state.

Class Methods
Instead of accepting a self parameter, class methods take a cls parameter that points to the class—and not the object instance—when the method is called.

Because the class method only has access to this cls argument, it can’t modify object instance state. That would require access to self. However, class methods can still modify class state that applies across all instances of the class.

Static Methods
The third method, MyClass.staticmethod was marked with a @staticmethod decorator to flag it as a static method.

This type of method takes neither a self nor a cls parameter (but of course it’s free to accept an arbitrary number of other parameters).

Therefore a static method can neither modify object state nor class state. Static methods are restricted in what data they can access - and they’re primarily a way to namespace your methods.


Auf dieser Seite können Sie das Online-Video Instance method, class method and static method | Python tutorial for absolute beginners in Hindi mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer CodeWithRajat 09 März 2021 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 2,378 Mal angesehen und es wurde von 38 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!