Python learning diary 5

Python learning diary 5

- classes and objects (2)

404 yesterday, one day did not do what matter to children, but also to learn unthinking, today answers to some doubts.

In the end what is the object?

 We say, an abstract class is a group of objects, classes can be abstracted into a concept; the presence of an object is a specific entity; for example, "car model" is a class, then the "jeep", "motorcycle" is the object, we are talking about a person who is the object, not humans.
 Most of the time, define a class to create objects of the class repeat the same multiple objects of a class have the same characteristics, but class defines the common features of multiple objects. From a certain perspective, the classes are defined is characterized in a plurality of objects, and therefore not a specific class entity exists, the object is the presence of a particular entity.

Then I started senseless. I'm starting to rip off obsessed why not use the name of the class to call the method directly? Carefully read it again today, yesterday really is sleepwalking? 404 No Found

 In fact, everything Python program processing is a kind of object, the difference between abstract and concrete just to better understand the relationship between the two only, not its definition. Like functions, modules and classes such programming unit in Python are objects, they are created by such statements and expressions def, class, import and lambda, and can be freely passed between the scripts, stored in other objects.

(Goose is Zhendi stupid, evil is also learned a little JAVA person, class time dreaming? .. dumb)

4.2 Method

 The method is an abstract class or behavioral characteristics of the object, but the method is actually Python function, defined methods and functions are very similar.

4.2.1 class can also call an instance method

 Python class method defined default is an instance method, instance method can be invoked through the object after creating objects.

 Python classes like namespace. Python is the default program within the global namespace, class-based material is within the namespace. Python allowed to stand globally executable code, executable code based material allowed to stand in a class range.

# 定义全局空间的fun函数
def fun():
    print("全局空间内的fun方法")
# 定义全局空间内的bar变量
bar = 20
#-------------------
class ssalc:
    # 定义类体内的fun函数
    def fun(self):
        print("类体空间内的fun方法")
    # 定义类体内的bar变量
    bar=2000
#----------------------
# 调用全局空间的函数和变量
fun()
print(bar)

# 调用类空间内的函数和变量
s=ssalc()
ssalc.fun(s)
print(s.bar)

 Python class instance can invoke a method, but using the class method calls the instance, Python is not automatically self Method of binding a parameter the parameter value, the program must be explicitly passed in the caller method as the first parameter. The last case of the above code. Like the following code will error:

ssalc.fun()
TypeError: fun() missing 1 required positional argument: 'self'

That is where I had to rip off circle here, why not directly use the class name to call, but the actual parameter passing problems caused.

4.2.2 class methods, and static methods

 Python supports the definition of class methods, even support the definition of static methods. Class methods and static methods are very similar, the difference between them is that: Python automatically binds the first argument class method, the first parameter class method (usually named cls) are automatically bound to the class itself; but for static the method is not automatically binding.
 @classmethod modified method is the class method;
use @staticmethod modified method is a static method.

class player:
    @classmethod
    def classM(cls):
        print('类方法:',cls)
    @staticmethod
    def staticM(p):
        print('静态方法:',p)
#调用类方法,player类会自动绑定到第一个参数
player.classM()

#调用静态方法,不会自动绑定,需要手动绑定
a=player()
a.classM()  #类方法不需要手动绑定
#用对象调用staticM静态方法,实际上依然还是使用类来调用的
#必须为第一个参数执行手动绑定
a.staticM('hello')  

4.2.3 @ function decorator

 The above and @staticmethod @classmethod essentially decorative function, wherein classmethod and staticmethod python are built-in functions.
 Use the @ symbol to refer to the existing function can be used to modify other functions, modified decorative function.
 When using the program "function by @A" Another decoration "B function", in fact, completed the following two steps:

  1. B will be modified as a parameter to a function of the @ symbol references a function of A;
  2. The decorative function to return the value B in the first step.
def funA(fn):
    print('A')
    return 'fkit'

@funA  #相当于把funB作为funA的参数,funA(funB)
def funB():
    print('B')
print(funB)
A
fkit  #因为funA的返回值是fkit,所以此时funB=fkit,
       # funB相当于被替换成了字符串

 Function is modified by the @ sign is a very useful feature, both add some additional processing logic on the front modified as a function of the permission check may be added some additional processing logic as modified after the logging function you can also do some repair operations when the target method exception.

Three object-oriented language features: polymorphism, inheritance and encapsulation:

More than 4.3 state

Polymorphism is the core of the Python programming mode, a funny name for it called: duck type. This comes from a saying: "If it walks like a duck and quacks like a duck, then it is a duck."

 When you do not know what type of object, what kind of operation it can perform, are polymorphic at work. The Python variable declaration does not require a new variable types, variables can be used directly; Another example is the addition operator "+" both for the number of strings can also be used; and if a polymorphism may be referred to a master the repr function (), which can express a target string out to identify, it can be configured to indicate a function of the length of the object:

def length_fun(x):
    print('The length of',repr(x),is,len(x)

This function would apply to any output of any object.

If you want to destroy the multi-state, the only way is to use explicit type checking, such as type, issubclass, isinstance and other functions, but in fact issubclass itself is polymorphic up.

4.4 package

 Package (Encapsulation) refers to the outside hiding unnecessary details.
 This seems to be the same polymorphism, without knowing the internal details of the object you can execute it, the two concepts like, because they are abstract principles .

  Encapsulation is an object-oriented language simulation of the objective world, in the objective world, the status information of the object are hidden inside the object, the outside world can not directly manipulate and modify. A class of objects or packages may be implemented to achieve these purposes:

  • Implementation details behind class
  • Allow only access to the data in advance by a predetermined method, the control logic can be added in the process, the access attribute unreasonable limits.
  • Data integrity can be checked, the object will help to ensure the information
  • Easy to modify, easy maintenance code

In order to achieve good encapsulation needs to be considered from two aspects:

  • The implementation details of the object attributes and hide, does not allow direct access to external
  • The method exposed, so safe method to control access to these attributes and operations.
    The possession of the hide, exposed to the dew, like wearing the same clothes hhhh

JAVA and Python does not provide the same priviate modifiers, so Python can not really support hidden. Python method is hidden: the members of the class name starts with a double underline, Python will hide them.

class User :
    def __hide(self):
        print('示范隐藏的hide方法')
    def getname(self):
        return self.__name
    def setname(self, name):
        if len(name) < 3 or len(name) > 8:
            raise ValueError('用户名长度必须在3~8之间')
        self.__name = name
    name = property(getname, setname)
    def setage(self, age):
        if age < 18 or age > 70:
            raise ValueError('用户名年龄必须在18在70之间')
        self.__age = age
    def getage(self):
        return self.__age
    age = property(getage, setage)
# 创建User对象
u = User()
u.name = 'fkit'
# 引发 ValueError: 用户名长度必须在3~8之间

User class instance variables __name and __age two were hiding, this program can not access these variables directly, only through setname (), getname (), setage (), getage () These accessor methods access, and setage (), setname () will set the user's name, age control, only the qualified are allowed to set.

 The above code also defines a __hide () method, which is hidden by default, if a program attempts to call the following code will throw an error:

#尝试调用隐藏的__hide()方法
u.hide()
AttributeError: 'User' object has no attribute '__hide'

But Python is actually no real hiding mechanism, double underline just a little trick: Python secretly change to the method name double underscore, it will add a single underscore and the class name before the method name, so you can call in accordance with the following manner :( but usually do not do)

# 调用隐藏的__hide()方法
u._User__hide()

4.5 class inherits

 Inheritance is an important means lazy. Python inheritance is multiple inheritance mechanism, that is a subclass can have more direct parent at the same time.

4.5.1 inheritance syntax

 Python syntax subclass inherits the parent class is a subclass is defined in the parent class in parentheses after multiple sub-class, you can show that the subclass inherits the parent class:

class SubClass(SuperClass1,SuperClass2,...):
     #定义部分

If you do not explicitly specify the tired parent when defining a class, this class inherits from the Object class, so to say, the Object class is the parent class of all classes, either direct or indirect parent parent class.
 Usually scope than the parent class contains a large subclass, expand the subclass is the parent class, parent class to subclass, the subclass is a special kind of parent.

4.5.2 override the parent class

Most of the time, a subclass of the total to the parent class basis, and then be expanded to the parent, but the subclass is also possible that the parent class is not that great either parent is not suitable sub-class, the Yangtze River superseding the old, subclass the method of the parent class will need to be rewritten.

class Driver: 
    # Driver类的fly()方法
    def driver(self):
        print("我会开拖拉机...")
class Ostrich(Driver):
    # 重写Driver类的driver()方法
    def driver(self):
        print("我会开大卡车,挖掘机,拖拉机,直升机...")
  
# 创建Ostrich对象
os = Ostrich()
# 执行Ostrich对象的driver()方法,将输出"我会开大卡车,挖掘机,拖拉机,直升机..."
os.driver()

 This subclass comprises the same name as the parent class method with the phenomenon known method overrides (Override), also called covered method.

4.5.3 Use unbound method calls the overridden method

 If the method call after rewriting in a subclass, Python will always be executed subclass overrides the method, the method of the parent class is rewritten is not performed, if need be rewritten can be called the father of the child in the class when the method, you can use the class name to call the call.

class BaseClass:
    def foo (self):
        print('父类中定义的foo方法')
class SubClass(BaseClass):
    # 重写父类的foo方法
    def foo (self):
        print('子类重写父类中的foo方法')
    def bar (self):
        print('执行bar方法')
        # 直接执行foo方法,将会调用子类重写之后的foo()方法
        self.foo() 
        # 使用类名调用实例方法(未绑定方法)调用父类被重写的方法
        BaseClass.foo(self)
sc = SubClass()
sc.bar()
4.5.4 super constructor function calls the parent class

Python subclass also inherited parent class constructor, if the sub-class has more than one parent class, then the top surface of the construction method of the parent class will be used to, for example:

class Employee :
    def __init__ (self, salary):
        self.salary = salary
    def work (self):
        print('普通员工正在写代码,工资是:', self.salary)
class Customer:
    def __init__ (self, favorite, address):
        self.favorite = favorite
        self.address = address
    def info (self):
        print('我是一个顾客,我的爱好是: %s,地址是%s' % (self.favorite, self.address))
# Manager继承了Employee、Customer
class Manager (Employee, Customer):
    pass
#m = Manager(25000)
m = Manager('IT产品', '广州')
#m.work()  #代码1
m.info()  #代码2

Manager class code defined in the class inherits the Employee and Customer classes, because Employee standing in the front so will give priority to the use of his constructor. So with the Manager (2500) Manager to create an object is, the method initializes the instance variable salary, so 1 can execute code, but the code 2 will error, because the program did not initialize favorite and address two variables.

 In order for Manager can initiate two parent classes at the same time, it should define its own constructor - is to rewrite the constructor of the parent class. Python requirement: If the child class overrides the parent class constructor, then the subclass constructor must call the constructor of the parent class; there are two ways:

  • Use unbound method
  • Using super () function call the parent class constructor

 super is actually a class, call the super essence it is to call the constructor to create a super super class of objects.
 Using super () constructor method most commonly used is not pass any parameters, then the object's methods may be super instance method call parent class, the class parent class may be invoked. . When you call an instance method will complete the first argument self binding when calling a class method will complete the binding of the first argument cls.
Then the above procedure will be changed to the following form:

class Manager(Employee, Customer):
    # 重写父类的构造方法
    def __init__(self, salary, favorite, address):
        print('--Manager的构造方法--')
        # 通过super()函数调用父类的构造方法
#        super().__init__(salary)
        # 与上一行代码的效果相同
        super(Manager, self).__init__(salary)
        # 使用未绑定方法调用父类的构造方法
        Customer.__init__(self, favorite, address)
# 创建Manager对象
m = Manager(25000, 'IT产品', '广州')
m.work()  #①
m.info()  #②

5.6 Python's dynamic

Python is a dynamic language, in: classes, objects, properties, methods can dynamically add and modify.

5.6.1 Dynamic Properties and __slots__

 Dynamically add an object method is only valid for the current object, if you want to add all instances method, the method can be achieved by adding a class, but will also bring some risks.
 If you want to increase the limit for a dynamic properties and methods that you can use to specify the property __slots__.

class Dog:
    __slots__ = ('walk', 'age', 'name')
    def __init__(self, name):
        self.name = name
    def test():
        print('预先定义的test方法')
d = Dog('Snoopy')
from types import MethodType
# 只允许动态为实例添加walk、age、name这3个属性或方法
d.walk = MethodType(lambda self: print('%s正在慢慢地走' % self.name), d)
d.age = 5
d.walk()

#如果尝试添加其他属性,那么会报错
d.foo = 30 # AttributeError

Note there are two points:

  • __slots__ not limited to dynamically add properties by class property or method
  • __slots__ current class instance attributes only acts, there is no effect on the derived subclasses need to redefine __slots__ property in the subclass.

5.6.2 Use metaclass

 # If you want to create a batch class all have certain characteristics can be achieved by mataclass. Use metaclass to dynamically modify the class definition when creating classes.
In order to dynamically change the class defined metaclass, the program must first define the metaclass, metaclass should inherit the class type, and rewritable __new __ () method.

5.6.3 use type () to dynamically create class

 type () function can be used to view the variable's type:

class Role:
    pass
r = Role()
# 查看变量r的类型 
print(type(r)) # <class '__main__.Role'>
# 查看Role类本身的类型
print(type(Role)) # <class 'type'>

Visible, the type of the class itself is type.
When the program uses the class create a class, can be understood as defining a special object (the object type class), and speak to copy the object to the Role variable, all instances of the class with the class definitions are type classes.
 It is possible to dynamically create a class with the type () function:

def fn(self):
    print('fn函数')
# 使用type()定义Dog类
Dog = type('Dog', (object,), dict(walk=fn, age=6))
# 创建Dog对象
d = Dog()
# 分别查看d、Dog的类型
print(type(d))
print(type(Dog))
d.walk()
print(Dog.age)
<class '__main__.Dog'>
<class 'type'>
fn函数
6

When you create a class using type () can specify three parameters:

  • Class name created: a parameter
  • A collection inherited parent class: Parameter Two
  • Parameters III: The dictionary object class variables and methods of the class bindings.

Several code directly with the mad python inside a lecture, really do not want to write
tomorrow must be a good review ah
autistic up. . . . . . . . . . . . .
The autistic. . . . . . . . . . . . .
The autistic. . . . . . . . . . . . .

Released five original articles · won praise 0 · Views 229

Guess you like

Origin blog.csdn.net/weixin_44204830/article/details/104283928
Recommended