官术网_书友最值得收藏!

Static, instance, and class methods in Python

There are three kinds of methods that can be defined within a Python class. Up until now, we have mostly been dealing with instance methods, which we have invoked with our Python class instances:

  • Instance methods and variables: Any method defined within a Python class that is invoked with the instance of the class, taking the self as its first positional argument, is said to be instance method. An instance method is able to access the  instance variables and the other instance methods of the class. With the self.__class__ construct, it is also able to access the class level variables and the methods as well. An instance variable, on the other hand, is any variable that is declared within the Python class with the self keyword.
  • Class methods and variables: Any method that is declared with the @classmethod Python decorator invoked with the class name is said to be a class method. A class method may also be declared without the @classmethod decorator. If this is the case, it must be invoked with the class name. A class method will have access only to the variables that are marked or declared at the class level and will not have access to object or instance level class variables. A class variable, on the other hand, can be declared outside any method. Within the class, we have to declare the variable without using the self keyword. For this reason, class variables and methods to some extent resemble static methods and variables that we studied in Java, but there is a catch, as mentioned here:
In Java and C#, we know that a static variable cannot be accessed with an instance of the class. In Python, static variables are class-level variables and they can actually be accessed by the instance of the class. But the access is read-only access, such that whenever a class-level variable is accessed with an instance of the class and the instance tries to modify or update it, Python automatically creates a new copy of the variable with the same name and assigns it to this instance of the class. This means that the next time the variable is accessed with the same instance, it will hide the class-level variable and it will provide access to the newly created instance-level copy of it instead.
  • Static methods: Any method in a Python class that is declared with the @staticmethod decorator is said to be a static method. Static methods in Python are different from what we saw in Java and C#. A static level method doesn't have access to the instance  or the object-level variables, nor to the class-level variables of the class.

Let's take an example called Class_methods.py to explain this further:

A continuation of the preceding code is shown as follows:

The preceding code snippet explains the use of static, instance, and class methods. Whenever a class method is invoked by the instance of the class, Python automatically translates the instance type to class type internally, which can be seen in line 42. 

The output is as shown in the following screenshot:

主站蜘蛛池模板: 全椒县| 凤冈县| 通榆县| 阜新市| 钦州市| 临漳县| 田林县| 汾阳市| 海丰县| 方正县| 桐柏县| 阿合奇县| 潍坊市| 萨嘎县| 日喀则市| 商都县| 英山县| 乌鲁木齐县| 万源市| 南通市| 邳州市| 绥芬河市| 满城县| 芦溪县| 永安市| 贵溪市| 如东县| 天祝| 黄陵县| 巴林右旗| 永寿县| 黑山县| 安西县| 日照市| 赣榆县| 安顺市| 大化| 永德县| 仙居县| 普安县| 河津市|