python object-oriented

高洛峰
Release: 2016-12-01 13:36:21
Original
1088 people have browsed it

Members in a class

1. Fields

1. Static fields Accessed through the class itself Static fields are created when the class code is loaded, no need to wait for new class name()

2. Ordinary fields accessed through objects

2 .Methods

All methods belong to classes

1. Ordinary methods can only create an object of the class first, and then call it through the object def show(self): self is the class object that automatically passes the calling method

print(self .name)

2. Static method The calling of static method does not depend on any object. It is called through class name.Method name (), saving memory.

Static method is a function of python, and the parameters do not need to be self. Any number of parameters

          @staticmethod

                                                                                                                                                                                                                                                                           ​Class name

. PASS 属 3. Properties

Fields and methods in the class

@property // Properties declare // Method Declaration

DEF ALL_PAGE (SELF): DEF All_PAGE (SELF):

Return 100 Return 100

@all_page.setter All_page must be a function defined under the decorator @property

def all_page(self,value):

pass

obj.all_page = 100 //Automatically call the setter decorator below Method


@all_page.deleter

def all_page(self):

pass

del obj.all_page //Automatically call the method below the delete decorator


 obj.all_page  property call like field Call the same method to get the return value of the function directly

          obj.all_page()                                  .

foo = property(func1, func2, func3)

func1, func2, fun3 are all methods defined in the class

class name.foo The program executes the func1 method and gets the return value of func1

class name.foo = " newvalue" Execute the func2 method


Member modifier

The default field and method name are public. If you add __ in front of the field and method, then the __name field becomes a private member

Private members are only classes It can be accessed by itself, and subclasses cannot access the private members of the parent class

Special members of the class

obj=Foo() Call the __init__ built-in method in the Foo class and create the obj object at the same time

obj( ) Foo()() Call the __call__ built-in method in the Foo class. This syntax only exists in python

__str__ function:

obj = Pager() print(obj) #<__main__.Pager object at 0x00000000006E3B00> ;

obj = Pager() print(obj.__dict__) #{'name':'333'} The output value is dict type data

Define a def __str__(self): return "{name: obj.name}"

print(obj) #{name:333} The output is the string information of the object instead of the memory address of the object


__dict__ function:

(obj is an object) obj.__dict__ Put any object Convert all the encapsulated data into dict

(Pager is a class) Pager.__dict__ Convert the member information contained in any class into dict

__getitem__(self,item) __setitem(self,key,value) __delitem__(self,key) function :

obj = Foo()

obj['aa'] #Automatically call __getitem__ in Foo class

obj['aa']=22 #Automatically call __setitem__ in Foo class

del obj[' aa'] #Automatically call the __delitem__

__iter__ function in the Foo class

An object cannot be iterated by default. If the class where the object is located defines the __iter__ function, then the object can be iterated

isinstance ret=isinstance(obj,Foo) Check whether the object obj is an object of class Foo or Foo's parent class

issubclass ret=issubclass(Foo,Bar)

The functional difference between super and decorator (for Code extension)

1. You can use decorators to add your own code to the function execution process originally written by others, but you need to add a decorator syntax sugar to the function

in the source code file.

2. Use super and reflection The mechanism allows you to add a subclass to inherit the class originally written by others without modifying the original code at all. Add super(cls).func to the

subclass custom method to add the original function to the existing code. .Realize code function expansion.

model = __import__(Path,fromlist=True) Path is the read configuration item string

cls = getattr(model,ClassName) ClassName is the read configuration item string

obj = cls()

obj.f1()            

Dynamically call specific functions in the python class through strings.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!