Package: folder, can have multilevel
Module: PY File
In Python, to avoid the names of different modules, you can put modules of the same name into different packages.
1. How to differentiate between packages and normal directories:
There must be a __init__.py file in each package, even if the file is empty
2. Use of new version codes in older versions:
From __future__ import xxx
Instance Properties
Since Python is a dynamic language, for each instance, it is possible to assign values directly to their properties, and the properties of the instance can be manipulated like normal variables.
1. Initializing Instance Properties
__init__ (self,xxx,...)
classPerson (object):def __init__(Self,name,gender,birth):#Initializing Instance PropertiesSelf.name=name Self.gender=Gender Self.birth=birthxiaoming= Person ('Xiao Ming','Male','1990-1-1', job='Student')Print(Xiaoming.name)Print(Xiaoming.job)
2. Arbitrary keyword parameters
Accept any of the keyword parameters: **kw
Initialization: self.__dict__.update (kw)
3. Access Restrictions
If an attribute starts with a double underscore, the property cannot be accessed externally
4. Example Methods
That is, the function defined in the class, the first argument is self.
An instance method is actually a property, which is actually a function object. For example, p1.get_grade Returns a function object, but this function is a function bound to an instance, andP1.get_grade () is the method call.
Within an instance method, all instance properties can be accessed, so that if the external needs access to private properties, which can be obtained through a method call, the form of the data encapsulation can simplify the external invocation in addition to protecting internal data consistency.
#-*-coding:utf-8-*-classPerson (object):def __init__(self, Name, score): Self.name=name self.__score=scoredefGet_grade (self):#instance method, which is the function ifSelf.__score>=90: return 'A-Excellent' elifSelf.__score>=60: return 'B-Pass' Else: return 'C-Inferior lattice'P1= Person ('Bob', 90)Print(P1.get_grade)#return Function ObjectPrint(P1.get_grade ())#Method Invocation
Class properties
If you bind a property to a class, all instances can access the properties of the class, and the class properties accessed by all instances are the same
1. Class attribute and instance property name conflict
When instance properties and class attributes have the same name, the instance property has a high precedence, which masks access to the class properties.
2. Class methods
By marking a @classmethod, the method binds to the person class, not to an instance of the class.
Because it is called on the class, not on the instance, the class method cannot get any instance variables, only the reference to the class is obtained.
classPerson (object):__count=0 @classmethod#to bind a method to a class defHow_many (CLS):returnCls.__count #The first parameter of the class method is passed into the class itself, usually named the parameter name CLS, and the above cls.__count is actually equivalent to Person.__count. def __init__(self,name): Self.name=name person.__count+=1Print(Person.how_many ())#private properties are not allowed outside access, but can be accessed through the class methodP1= Person ('Bob')Print(Person.how_many ())
python-Object-Oriented programming