Home  >  Article  >  Backend Development  >  Introduction to relevant knowledge of python classes (with examples)

Introduction to relevant knowledge of python classes (with examples)

不言
不言forward
2018-11-26 16:46:542265browse

The content of this article is an introduction to relevant knowledge about python classes (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Python-class attributes, instance attributes, class methods, static methods, instance methods

Class attributes and instance attributes

#coding:utf-8
class Student(object):
    name = 'I am a class variable' #类变量
>>> s = Student() # 创建实例s
>>> print(s.name) # 打印name属性,因为实例并没有name属性,所以会继续查找class的name属性
Student
>>> print(Student.name) # 打印类的name属性
Student
>>> s.name = 'Michael' # 给实例绑定name属性
>>> print(s.name) # 由于实例属性优先级比类属性高,因此,它会屏蔽掉类的name属性
Michael
>>> print(Student.name) # 但是类属性并未消失,用Student.name仍然可以访问
Student
>>> del s.name # 如果删除实例的name属性
>>> print(s.name) # 再次调用s.name,由于实例的name属性没有找到,类的name属性就显示出来了
Student

Class methods, instances Method, static method

Instance method, the first parameter must be passed by default to the instance object, and it is generally customary to use self.
Static method, no parameters required.
Class method, the first parameter must be passed by default, and cls is generally used.

# coding:utf-8
class Foo(object):
    """类三种方法语法形式"""
 
    def instance_method(self):
        print("是类{}的实例方法,只能被实例对象调用".format(Foo))
 
    @staticmethod
    def static_method():
        print("是静态方法")
 
    @classmethod
    def class_method(cls):
        print("是类方法")
 
foo = Foo()
foo.instance_method()
foo.static_method()
foo.class_method()
print('----------------')
Foo.static_method()
Foo.class_method()

Running result:

是类<class &#39;__main__.Foo&#39;>的实例方法,只能被实例对象调用
是静态方法
是类方法
----------------
是静态方法
是类方法

Class method

Since there can only be one initialization method in a python class, the class cannot be initialized according to different situations. Class methods are mainly used when a class is used to define multiple constructors.
Special note, static methods can also implement the above functions. When static methods have to write the name of the class every time, it is inconvenient.

# coding:utf-8
class Book(object):
 
    def __init__(self, title):
        self.title = title
 
    @classmethod
    def class_method_create(cls, title):
        book = cls(title=title)
        return book
 
    @staticmethod
    def static_method_create(title):
        book= Book(title)
        return book
 
book1 = Book("use instance_method_create book instance")
book2 = Book.class_method_create("use class_method_create book instance")
book3 = Book.static_method_create("use static_method_create book instance")
print(book1.title)
print(book2.title)
print(book3.title)

The above is the detailed content of Introduction to relevant knowledge of python classes (with examples). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete