Details and simple examples of the python class

  • 2020-05-27 06:12:42
  • OfStack

python class,


1. Class is a data structure that can be used to create instances. (1 in general, the class encapsulates the data and the methods available for that data)

2. The Python class is a callable object, that is, a class object

3. Classes are usually defined at the top of a module so that class instances can be created anywhere in the source code file defined by the class.

4. Instance initialization

instance = ClassName(args....) Classes can be instantiated using two special methods, s = s = s = s = s = s = s = s = s = s = s = s = s.

 class ClassName(base):

'class documentation string' # Class document string 
 class suite        # The class body 

base: a superclass is a collection of one or more parent classes for inheritance The body of a class can include: declaration statements, class member definitions, data properties, methods If the class does not have an inheritance relationship, base in parentheses is not provided

 class FirstClass():
  spam = 30    # Class data attribute 
  def display(self): # Class method 
   print self.spam

 x = FirstClass()   # Creating class instances 
 x.display()     # The method call 
 >>> 30
 >>> ['__doc__', '__module__', 'display', 'spam']

The class statement, like def, is executable code; The class is not created until the class statement is run Within the class statement, any assignment statement creates class attributes Each instance object inherits the class's properties and gets its own namespace

Python class methods and calls

The properties that the instance (object) contains

Callable property: method Data attributes

In OOP, instances are like records with "data," and classes are "programs" that process these records

Calling a method through an instance is equivalent to calling a method of the class to which it belongs to handle the current instance. For example, in the previous code example, x.display () is automatically converted to FirstClass.display (x), which calls the class's methods to handle the instance x Therefore, each method in the class must have an self parameter, which implies the current instance Assigning an self property within a method produces each instance's own property Python states that no method is allowed to be called without an instance, which is the concept of 'binding' (binding) The assignment statement in the class statement creates class attributes, such as spam in the previous example Assigning self, a special parameter passed to a method in a class method, creates an instance property

Python constructor

When an instance is created, Python will automatically call the method of s 57en__ in the class, providing the attributes to the instance with the option of invisibly

The method is called the constructor If no method is defined in the class, the instance will be created with a simple namespace. The first parameter of 1 must be self, and the self variable is used to refer to the instance bound by the method in the class instance method. Because an instance of a method is always passed as the first argument in any method call, self is selected to represent the instance. You must put self in the method declaration, but you can do it without using an instance (self). If self is not used in your method, consider creating a regular function, unless you have a specific reason for doing so. After all, your method code doesn't use an instance and doesn't associate its functionality with a class, which makes it look more like a regular function. In other object-oriented languages, self might be called this. S 70en __ cannot return any object Destructor: s 71en__ Constructors are necessary, and destructors can often be ignored (the Python interpreter will recycle itself)

class MyClass():
 def __init__(self, name): = name
  print 'My name is ' +
 def __del__(self):
  print + ' is dead.'

i1 = MyClass('Shaw')
>>> My name is Shaw
del i1
>>> Shaw id dead.


Use dir() or s 78en__ to view the properties of the class or instance S 79en__ : get the document string S 80en__ : get all the parent classes S 81en__ : the module in which the class is located S 82en__ : the name of the class to which the instance belongs

The variables available in the Python class methods

Instance variable: self. Variable name Local variables: variables created inside a method that can be used directly Static variables: variables defined in a class. Class name. Variable name Global variables: used directly


Inheritance describes how properties of a base class are 'inherited' to a derived class

A subclass can inherit any property of its base class, including data properties and methods A class that does not specify a base class has a base class called object by default Python allows multiple inheritance (you can inherit from multiple parent classes)

Thank you for reading, I hope to help you, thank you for your support of this site!

Related articles: