Python class

A method for the normal function of the class is only a special distinction - they must have an extra first argument name , its name is conventionally example self, self representatives of the class, not class.

self is not a python keyword, we can also put him into runoob normal execution of:

2.

python object is destroyed (garbage collection)

Python uses reference counting this simple technology to track and recover waste.

In the internal records of all Python objects use the respective numbers of references.

An internal tracking variables, called a reference counter.

When an object is created, it creates a reference count, when the object is no longer needed, that is to say, the object's reference count goes to zero, it is garbage collected. But the recovery is not "immediately" by the interpreter at the appropriate time, it will take up memory space garbage objects recovered. Python's garbage collector is actually a reference to a cycle counter and garbage collector

3.

Single underline, double underline, double underline head and tail description:

  • __foo__ : a special method is defined, system-defined names generally similar  __init __ () or the like.

  • _foo : starting with single underlined is protected types of variables, namely the protection type can only allow it to itself and the subclass access, can not be used  from module import *

  • __foo : double underline indicates the type of private (private) variables can only be allowed to visit the class itself.

4. new and init

When creating the object, init method is not the first to be called. But the new method

__new__ method accepts parameters and __init__ though is the same, but __init__ is called after the class instance creation, and __new__ method is the method to create the instance of the class, and returns an instance of the object to the init.

 p = Person (name, age) is first performed using the parameter name and age __new__ performing the method of the Person class, this method returns a __new__ instance of the Person class (usually using super (Persion, cls). __new __ (cls, ... ...) in such a way),

When we understand __new__ method, we can also use it to do some other interesting things, such as to achieve a singleton design pattern (singleton).

Because the process produced after every class is instantiated by __new__ controlled, so that by overriding __new__ method, we can achieve a very simple embodiment a single mode.

 

 

5.

 ※   difference __new__ and __init__, the statement is correct ? ( ABCD )

A. __new__ is a static method, and an example of a method __init__
B. __new__ method returns an instance is created, nothing is returned and __init__
C. Only when a __new__ return cls instance, can be called back __init__
D. calling __new__ When you create a new instance initialized with an instance when __init__


import time


class the Test ():
     '' ' static methods and class methods
    The constructor and destructor '' ' 
    Print ( ' the first to be printed ' )

    COUNT = 0    # class properties: classes and objects can call but can not modify the object class attribute

    DEF  __new__ (CLS, name, Age):   # new new method is a class method, 
        Print ( ' __new__ is the object created when a call is returned object to the init, and init is the self created after the completion of the call. ' )
         return Super (the Test, CLS). __new__ (CLS)

    DEF  __init__ (Self, name, Age):     # Self is the object of the current class itself, python interpreter will automatically put incoming objects. Examples of representatives of the class, not class 
        Print ( ' "the __init__" constructor, has the effect of initialization, that is, when the class is instantiated would perform the function ' )
        self.name = name
        self.age = age
        Test.count += 1

    @classmethod      # class methods: classes and objects can call. Class attribute only 
    DEF class_fun (CLS):
         Print ( ' class Fun% S ' % (Test.count))

    DEF EAT (Self):
         Print ( " It is an example of method ' )


      # Static method, only nominally classified management, in fact, can not access any property or class instance in a static method inside. But the objects and classes can be called 
    '' ' static methods and common way is not the same scope, only the current object class and calls the other are the same ' ''

    @staticmethod
    DEF static_fun ():
         Print ( ' static method ' )


    DEF  __del__ (Self):
         Print ( ' When del delete objects, will call his own destructor when the object is completed in another call a scope, the destructor will at the same time is out of its scope called once, which can be used to free up memory space. ' )



oo = Test('fan',19)

FF = the Test ( ' ZZ ' , 20)    # create two objects, then the count will increase to 2 

ff.count = 4
 Print (Test.count)
ff.static_fun()
Test.static_fun()
oo.class_fun()
print(oo.name)
Test.class_fun()

the time.sleep ( 5)   # call after five seconds to free up space del

 

class Test1():
    print('test1')

    the @Property                # the way into the same property call 
    DEF get_name (Self):         # This must be a return data. 
        return self.name

    @get_name.setter
    def get_name(self,name):
        self.name = name


t1 = Test1 ()
t1.get_name = 'fa'
print(t1.get_name)

 

Guess you like

Origin www.cnblogs.com/tarzen213/p/11079853.html