Python full stack development 9, object oriented, Meta class, and single case

Source: Internet
Author: User
Tags class definition

The previous series of posts are all about process-oriented programming, and now it's time for a wave of object-oriented explanations

First, Introduction

Object-oriented programming is a way of programming, using "Classes" and "objects", so object-oriented programming is actually the use of "classes" and "objects". A class is a template, the template can contain multiple methods (functions), the method to implement a variety of functions, objects are created from the template instance, through the instance, the object can execute the methods in the class, each object has the same method, but the data may be different.

Ii. classes, objects and methods

In Python, a class is defined by a class keyword, followed by class a class name, a word with a class name that is usually capitalized, followed by a class that inherits from that class, (‘要继承的类名‘) usually using a class if there is no suitable inheriting class. object This is the class that all classes will eventually inherit from, or they may not write.

Class MyClass ():             # creates the    def func (self):          # Definition Method        Passobj1=myclass ()               # Creates an object from MyClass

When defining a method in a class, it is used to implement a function just like a previously defined function, but when defining a method, you must pass at least one argument (self), which represents the created object, and the function does not need to look at what the self specifically refers to.

Class MyClass ():    def func (self,str):        print (SELF,STR)    def func1 (self,str):        passobj1=myclass ()          # Self which object is called, Self is that object  obi <====> selfprint (obj1)             # <__main__. MyClass object at 0x000002c2119ad4a8>obj1.func (' Jason ')      # <__main__. MyClass Object at 0x000002c2119ad4a8>

As can be seen from the data printed above, the address value of the created Obj1 object is the same as the address value of the self in the Func method, which means that self is the object created by the creator, and the reader must understand what self refers to.

Third, object-oriented three major characteristics, encapsulation, inheritance and polymorphism. 1. Encapsulation

Object-oriented has 3 major features, first of all we first feature, encapsulation, encapsulation is generally by encapsulating the data in the class, and through the object or self to get. Similar to other object-oriented languages, data encapsulation is done through constructors. Here's a look at the code.

Class A:    def __init__ (self,name):        # constructor, initialize data,        self.name=name              # Encapsulate Data    def F1 (self):        Print (self.name)            # Gets the encapsulated data through self a=a (' Jason ') a.f1 ()                              #通过对象获取封装数据

There is also a way to encapsulate the data using private properties, to look at the specific usage,

Class A:    name= ' Jason '    __age=18                        # private static field,    def __init__ (self):        self.__like= ' soccer '        # private Normal field        self.hobby= ' kkkk '    def F1 (self):        print (self.__age)         # private static field, private Normal field can only be called by methods in the class        print (self.__ Like) # A.__age                         # outside gets no private static field, data is encapsulated A=a ()                             # soccera.f1 ()                            # 18print (a.hobby)
2. Inheritance

Python inheritance can be multiple inheritance, through inheritance, you can get the function of the parent class, when inheriting, if there are duplicate methods in the parent class, first find yourself, if there is the following relationship, D inherit b,e inherit c,f inherit D,e, then the lookup order, D->b->e->c, If you have the following relationship, B inherits A,c inheritance A,d inheritance b,e inheritance c,f inheritance d,e, then look in order, d->b->e->c-a, take a look at the usage

Class A:    def f (self):        print (' A ') class B:    def f (self):        print (' B ')    def F1 (self):        print (' bbbb ' ) class C (A, A, b):    def F1 (self):        print (' C ') cc=c () cc.f ()         # ACC.F1 () #        C # Below is the difficult point class A:    def Bar ( Self):        print (' bar ')        self.f1 () class B (A):    def F1 (self):        print (' B ') class C ():    def-F1 (self):        print (' C ') class D (B):    def F1 (self):        print (' D ') class E (c,d):    Passd=d () D.bar ()             # D.bar (). F1 () Where to find F1?

In addition to inheriting methods, you can also inherit the constructor of the parent class

# Inheritance Construction Method Class A:    def __init__ (self):        self.name= ' Jason ' class B (A):    def __init__ (self):        self.age= ' + '        super (b,self). __init__ ()        # a.__init__ (self)          #另一种继承构造函数的方法d =b ()
3. polymorphic

Python natively supports polymorphism, so it doesn't make sense to talk about polymorphism in Python-oriented objects, and there's not much to say

Iv. members of the class

The members of the

class can be divided into three main categories: fields, Methods, and properties, and the following is a look at the usage from the code

Class a:country = ' China ' # static field saved in class save memory def __init__ (self, name): Self.name = Name     # Normal field, save in Object Def Show (self): # normal method, save in class, object through class object pointer using print (' normal method ') return Self.name @staticmethod # static method, save in class Def F1 (): # no self parameter print (' static method ') @     The Classmethod # class method is equivalent to a static method with a class name parameter Def f2 (CLS): # Pass the current class name as a parameter into print (' class method ') @property # Property or attribute, the normal method is disguised as a field, but cannot pass in the second parameter Def f3 (self): print (' property ') return sel F.name @f3. Setter # Set Property def f3 (Self,value): print (value) self.name= Valuea = A (' Jason ') Print (Hasattr (A, ' name ')) # Falseprint (Hasattr (A, ' name ')) # Trueprint (Hasattr (A, ' show ')) # Trueprint (Hasattr (A , ' Country ') # Trueprint (Hasattr (A, ' country ')) # true# First, go to your own member in addition to the method in the class # in the following way to access the: static field, static method, class method (static State method plus a class name parameter) # through object accessThere are: normal field, normal method # has self, object call # No Self, Class call A.F1 () # static Method A.show () # normal method A.f                        2 () # class Method A.f3 # Property, get property value execution without parentheses, and object call normal field similar to a.f3= ' Kobe '                        # Set the property value, print (a.country) print (a.name) # Private can only be called in the class, can no longer call class A:name= ' Jason ' __age=18 # private static field Def __init__ (self): self.__like= ' Soccer ' # Private normal field self.hobby= ' KKKK ' def F                         1 (self): print (self.__age) # private static field, private Normal field can only be called by method in class print (self.__like) # A.__age           # External get not private static field A=a () # SOCCERA.F1 ()

Special members, not much to say, directly on the code.

Class A:def __init__ (self): print (' init ') self.name= ' Jason ' def __str__ (self): return ' ....    .‘ def __call__ (self, *args, **kwargs): Print ("call") return 1 def __getitem__ (self, item): Print (ITE M,type (item)) def __setitem__ (self, Key, value): Print (Key,value) def __delitem__ (self, Key): Print (ke               Y) def __iter__ (self): yield 1 yield 2a=a () # The Init class is appended with parentheses to execute the __init__a () # The Call object is appended with parentheses to execute __call__print (a) # Execute __str__ Method K=a () () # init call k=1a[' DD DD '] # execution __getitem__ <class ' str ' >a[' name ']= ' Jason ' # execution __setitem__del a[' name '] # execute __DELITEM__A[1:3] # py2__getslice__,py3 execute __getitem__ <class ' slice ' ; a[1:3]=[1,2] # Py2__setslice__,py3 execute __setitem__del a[1:3] # Py2__detslice__,py3 Execute __delitem__p  Rint (a.__dict__)     # {' Name ': Field inside ' Jason '} object Print (a.__dict__) # class inside field for I in a: # Execute __iter__ print (i) 
Five, meta-class

In Python everything is an object, and a class is an object, so the question is how the class is created, the definition of the class is created dynamically at runtime, and the way to create class is to use the type() function, the type() class created by the function and the direct write class are exactly the same, Because the Python interpreter encounters the class definition, it simply scans the syntax of the class definition and then invokes the type() function to create the class. The type can accept a description of a class as a parameter, and then return a class that can be used like this:type(class name , tuple of the parent class (can be empty for inherited cases), dictionary containing the property (name and value))

# By type custom class Def F1 (self):    print (self.name) myclass=type (' MyClass ', (), {' name ': ' Jason ', ' F1 ': F1}) print (MyClass, Type (MyClass))                  # <class ' __main__. MyClass ' >, <class ' type ' >print (myclass.name)                           # Jasonmy=myclass () print (my.__class__)                           # <class ' _ _main__. MyClass ' >print (my)                                     # <__main__. MyClass object at 0x000001f3013751d0>my.f1 ()                                       # jason

Having said the type, what exactly is the type and the meta-class, and the type is actually a meta-class. The type is the meta-class that Python uses to create all the classes behind it, in addition to type() dynamically creating classes, to control the creation of classes using Metaclass (meta-Class), we want to create classes, then create classes based on Metaclass, So: Define Metaclass First and then create the class. The connection is: Define Metaclass First, you can create the class, and finally create the instance. The general function of a meta-class is to intercept the creation of the class, modify the class, and return the modified class. Let's take a look at the use of code

# def upper_attr (Future_class_name, future_class_parents, Future_class_attrs) with functions: # attrs= ((name, value) for Name,v Alue in Future_class_attrs.items () if isn't Name.startswith (' __ ')) # Uppercase_attr=dict ((Name.upper (), value) for Name,v  Alue in Attrs) # return type (Future_class_name,future_class_parents,uppercase_attr ()) # # class A (): # # __METACLASS__ = upper_attr# country= "China" # def __init__ (self): # self.name= ' Jason ' # print (hasattr (A, ' Country ')) # Prin The parameters received by the T (hasattr (A, ' country ')) ' __new__ () method are: The object of the class that is currently being created, the name of the class, the collection of parent classes inherited by the class, and the collection of methods for the class. __new__ is a special method called before __init__ __new__ is the method used to create the object and return it, and __init__ is used to initialize the passed parameters to the object. You seldom use __new__ unless you want to be able to control the creation of objects here, the object being created is the class , we want to be able to customize it, so we rewrite __new__ here if you want, you can do something in __init__ and there are some high-level usages that involve rewriting __call__ special methods, but we don't use the class Uppmetaclass here ( Type): Def __new__ (CLS, name,base,attrs): Uppattrs = Dict ((K.upper (), V) for k,v in Attrs.items () if not K.STARTSW      ITH (' __ ')) # return type.__new__ (CLS, name, Base, Uppattrs)  Return Super (Uppmetaclass, CLS). __new__ (CLS, name, Base, Uppattrs) class A (Metaclass=uppmetaclass): # __metaclass__ = Uppmetaclass country= "China" def __init__ (self): self.name= ' Jason ' Print (Hasattr (A, ' country ')) print (Hasattr (A, ' Country ')) print (a.country)
Vi. Single-Case mode

Singleton, as the name implies a single instance, the purpose of simple interest mode is to ensure that there is only a single instance in the current memory, to avoid memory waste, the following directly see how to implement a single case

Class Instance: # method One __instance=none # private static field Def __init__ (self): Self.name= ' Jason ' self.passwd= ' kkkkk ' @staticmethod def get_instance (): If not Instance.__insta               NCE: # If the private static field is empty, create an instance instance.__instance = Instance () return instance.__instance             # is not empty, just return an instance obj1=instance.get_instance () # Executes a static method, creates an instance assignment to Obj1obj2=instance.get_instance () # Execute a static method to assign a private static field saved object to Obj2print (obj1) # <__main__. Instance object at 0x0000021aedf56048>print (obj2) # <__main__.        Instance object at 0x0000021aedf56048>class Singleton (): # Method Two Def __new__ (CLS, *args, **kwargs): If not hasattr (CLS, ' _instance '): Cls._instance=super (SINGLETON,CLS). __new__ (Cls,*args, **kwargs) retur N cls._instanceclass A (Singleton): Passa=a () b=a () print (A is B) class SingletoN (Type): # method Three Def __call__ (CLS, *args, **kwargs): If not hasattr (CLS, ' _instance '): C        Ls._instance = Super (Singleton, CLS). __call__ (*args, **kwargs) return Cls._instanceclass A (Metaclass=singleton): # __metaclass__ = Singleton Passa = A () b = A () print (A is B)
Vii. Supplementary

Another way to express a property:

Class A:    def __init__ (self):        self.mylist = [1, 2, 3, 4]    def getmylist (self):        print (self.mylist)    def setmylist (self, value):        self.mylist.append (value)        print (self.mylist)    def delmylist (self):        if Len (self.mylist) > 0:            self.mylist.pop ()        print (self.mylist)    My_pro_list = Property (Getmylist, Setmylist, Delmylist, ' property ') A = A () A.my_pro_lista.my_pro_list = 7del a.my_pro_list "    [1, 2, 3, 4]    [1, 2, 3 , 4, 7]    [1, 2, 3, 4] "

Simple Interest Supplement:

Class Singmetaclass (Type):    def __call__ (self, *args, **kwargs):        if isn't hasattr (self, ' instance '):            # Self.instance = Super (Singmetaclass, CLS). __new__ (CLS, name, Base, Attrs)            # self.instance = self.__new__ (self, *args , **kwargs)   # This is OK, but there is no comment on the good understanding            self.instance = Super (). __call__ (*args, **kwargs)        return Self.instanceclass A (metaclass=singmetaclass):    Pass

PY27 inheritance order, there is a situation and py3 different, such as, where bar does not inherit object (classic Class), depth first, other cases, are breadth first

  

# Py27class Bar ():    def F1 (self):        print (' Bar ') class A (bar):    def f (self):        print (' a ') class C (a):    def f (self):        print (' C ') class B (bar):    def F1 (self):        print (' B ') class D (b):    def F1 (self):        print (' d ') Class E (c,d):    def f (self):        print (' e ') e = e () e.f1 ()

Abstract classes and Abstract methods:

From ABC import abcmetafrom ABC import abstractclassmethodclass absclass (Metaclass=abcmeta):      # Abstract class + abstract method, similar interface, constraint class            @abstractclassmethod    def F1 (self): pass    @abstractclassmethod    def f2 (self): Passclass B (Absclass): # B needs to fully implement abstract methods in abstract classes    def F1 (self): pass    def f2 (self): Pass          # If a method in class B is not fully implemented in the Absclass class, it can be compiled, but execution will be an error 
   
    def F3 (self): PASSC = B ()
   

  

  

  

  

Python full stack development 9, object oriented, Meta class, and single case

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.