python4-Reflection _ Class

Source: Internet
Author: User

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

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.