Python basic knowledge of the whole network the most complete 5 (Classes and Objects)

Five Classes and Objects

1. Definitions and Generation

definition:

class Teacher:              #Python中的类名**约定以大写字母开头**
     age = 20               #属性
     weight = '10'
     def run(def):          #方法
     print('老师正在努力的跑…')
     def sleep(def):
          print('老师正在睡觉…')

inherit:

class Demo(People):       #表明Demo类 *继承* 了People类。
     pass                 #pass表示不做任何事
classDemo(Test1,Test2…)   #可以 *多继承* ,逗号隔开
     pass

Generation:

Libai = Teacher()

2. syntax details

  • 1. __init __ (Self, param1, param2 ...) :
    Constructor #self equivalent of this in Java

# Create is called automatically when the function and the parameters must correspond

  • 2. Private variables / methods :

Precede the variable name or function name "_" is the private two underscores
is actually the variable name to: _ _ class name variable name
with a single underscore can still call outside

  • 3. Super () : Usage: super () ._ init_ () # will automatically find the parameters parent class method and pass in the required

  • 4.issubclass(class,classinfo):

If the first parameter is a second sub-type of parameter returns True, False otherwise
ClassInfo may be a tuple class of objects, which is a subclass of any class, i.e. returns True
any subclass of object classes are

  • 5.isinstance(object,classinfo):

Check whether an instance of an object belongs to class. Precautions classinfo supra
check whether an object (object) belonging to the class classinfo

  • 6. The the hasattr (Object, name) : a test subject (object) whether a specified attribute (name) (in quotes)

  • 7. getattr (Object, name) : If there is no default on the print string

  • 8. setattr (Object, name, value) : If the property does not exist, create and assign this property

  • 9. delattr (Object, name) : delete objects in the specified attribute

  • 10.x = property(get,set,del):

Three parameters respectively property was obtained, setting properties, the implementation of a method to delete a property c1.x = 18; c1.x; these three functions are invoked when del c1.x, function of which is controlled operating parameters

  • 11. The __new __ (CLS [, ...]) : When a succession of immutable type, this method needs to be modified
    Example:
class CapStr(str):          #将创建对象是传入的字符串改为全大写
     def _new_(cls,string):
          string = string.upper()
          return str._new_(cls,string)
  • 12.__del__(self):

When the object is about to be destroyed, the object will automatically call this method. When there is no variable references this object, this method will call the object is recovered

  • 13. dict : in a manner to return the dictionary object attributes and attribute values    
    Example:
demo.__dict__

3. property access ( rectangle.py )

(1) __getattr__(self,name)             # 定义当用户试图获取一个不存在的属性时的行为

(2) __getattribute__(self,name)        #定义当该类的属性被访问时的行为

(3) __setattr__(self,name,value)       #定义当一个属性被设置时的行为

(4) __delattr__(self,name)             #定义一个属性被删除时的行为

4. descriptor ( MyProperty.py   temp.py )

  • Definition: descriptor is an instance of a particular type of property class assigned to another class

  • __ __get (Self, instance, owner) : used to access the property, he returns the value of the property

  • __ the __set (Self, instance, value) : will be called attribute assignment operation returns nothing

  • __ __delete (Self, instance) : Control delete operation does not return anything

The magic method

Format : __demo__ # This format ( front and rear double underlined ) are the magic method

Use :
For example:

class CapStr(str):        #将创建对象是传入的字符串改为全大写
     def _new_(cls,string):
          string = string.upper()
          return str._new_(cls,string)
  • (1) arithmetic operators

image

  • (2) inverse operation

Example:

class Nint(int):
     def_radd_(self,other):
          return int._sub_(self,other)

run:

a = Nint(5)
b =Nint(3)        # b相当于上述公式里的other

result:

a + b = 8
1 + b = 2

image

  • (3) incremental assignment operator

Incremental assignment operator

  • (4) a unary operator
    Unary operator

6. The container type of protocol ( CountList.py )

Container type of agreement

7. The method of container type magic

Container type magic method

Guess you like

Origin blog.csdn.net/affluent6/article/details/91535374