Reflection:
There are: demo.py with function foo ()
str1 = ' demo '
str2 = ' Foo '
module = __import__ (STR1)
Func = GetAttr (MODULE,STR2)
Example: usually applies to URLs
The Code directory structure is: Backend package below and account.py module inside and login function
data = raw_input (' Please enter address: ') #account/login
Array = data.split ('/')
Userspance = __import__ (' backend. ') +array[0])
Model = GetAttr (userspance,array[0])
Func = GetAttr (model,arry[1])
Func ()
Object-oriented:
Class:
class people (object):
Name= ' name ' This is a variable belonging to a class that is directly accessible to the class.
dict={} dictionaries are all instances that share a single copy of the data, a common
def __init__ (self,name,age,job): Initialization function (constructor), defining parameters. The variables here are private.
Self.name=name Self is an instance variable, and other functions can also call
Self.age=age
Self.job=job
Self.__name=name private variable, external cannot access
def walk (self): All you need to do is to do it.
Print "I am walking ..."
Self.__breath () can be called internally
Def talk (self):
Print "Talking with SB ...", Self.name
def __breate (self): Private method External cannot access, object cannot access
Pass
P1=people (' Dengminhui ', ' ten ', ' Enginner ') instantiated, passed in parameters
P1.walk ()
P1.talk ()
P1.__breath cannot call
Forcibly calling a private variable p1.__people.__name
Inheritance of the class:
Class Schoolmember (object):
def __init__ (self,name,age,sex):
Self.name=name
Self.age=age
Selef.sex=sex
def func (self):
Pass
Inherited
Class Student (Schoolmember):
def __init__ (Self,name,age,sex,grade):
schoolmember.__init__ (Self,name,age,sex) Here is the variable that inherits the parent class
Self.grade=grade after overriding the constructor, here is the variable added
def func (self):
Schoolmember.tell (self) function override
Add code
Multiple inheritance
Class School (object):
def __init__ (SELF,NAME,TEL,ADDR):
Self.tel=tel
Self.addr=addr
Self.stu_list=[]
Self.tech_list=[]
Class Student (Schoolmember,school):
static method:
Class MyClass (object):
Age = 22
def __init__ (self):
Self.name = "a"
def sayhi (self):
Print "---sayhi 1"
@staticmethod #静态方法 have nothing to do with the class, no need to instantiate to invoke, Class toolkit
Def sayhi2 (): No need to add self
Print "---sayhi 2", Self.name,self.age-----------> Self objects are not passed in, so they cannot be accessed by the calling
@classmethod does not need to be instantiated to invoke, cannot access instance data .... Instantiation can also call properties
def sayh3 (self):
Print "---sayhi 3" sself.name self.age cannot access Self.name, only class properties can be accessed
@property to turn a static method into a static property
def sayh4 (self):
print "----Sayhi4"
Return ' Test '
Call:
Myclass.syahi2 ()
M=myclass ()
M.SAYH4------> results will be test
The print m.__dict__ __dict__ method prints all the properties of the object.
New classes and Classic classes:
python4-Reflection _ Class