Python Full Stack development * 22 Knowledge Point Summary * 180703

Source: Internet
Author: User

22 Object-oriented---
properties, class methods, static methods, reflection
one. Properties
1. Attribute definition: The method is disguised as a property, although at the level of the code there is no advanced, but make it look more reasonable. (Methods similar to properties)
class Person:
def __init__ (self,name,weight,height):
def BMI (self):
return self.weight/self.height**2
P1=person ("Ji Zhe 嚞", 56,1.65)
# Print (P1.bmi ()) # 20.569329660238754 ( not added @property)
print (P1.BMI) # 20.569329660238754 (plus @property)
2. Since there are three types of access in the new class, we can define three methods for the same attribute according to the access characteristics of their several properties: Get, modify, delete
class Goods:
def __init__ (self,original_price,discount):
@property #获取
def Price (self):
return Self.original_price*
@price. Setter #修改
def Price (Self,new_price):
@price. deleter #删除
def Price (self):

G1=goods (5,0.80) # instantiates an object
print (G1.price ()) # No @property usage 4.0
Print (g1.price)# automatically executes functions under @property 4.0
g1.original_price=10 # Automatic execution @price.setter The following function changes the original price to ten
print (g1.price) # 8
del # Automatic execution @price.deleter the following function
print ( # ' Goods ' object has no attribute ' discount '

Two. Methods
methods include: Common methods, static methods, and class methods, three methods in memory belong to the class, the difference is that the calling method is different
1. Common method: Called by the object; at least one self parameter; When executing a normal method, the object that invokes the method is automatically assigned the
2. Class method: Called by the class, at least one CLS parameter; When the class method is executed, the class that invokes the method is automatically copied to the CLS; If the object calls the class method, it is automatically
passing the class space to the CLS does not pass the object space to the CLS.
def class_func (CLS): Defines a class method with at least one CLS parameter
class name. Class_func () Call class method
class A:
__name = ' Alex '
def func (self):
print (self, ' in Func ')

@classmethod # class method
def func1 (CLS):
print (CLS, ' in Func1 ')
a1=a ()
A1.func () #<__main__. A object at 0x0000011792f389e8> in func
A.func (A1) #<__main__. A object at 0x0000011792f389e8> in func
a.func1 () #<class ' __main__. A ' > in func1 automatically gives CLS class space
a1.func1 () #<class ' __main__. A ' > in Func1 object invocation is also an automatic class-transfer space
# What is the use of class methods?
# Direct The class to manipulate the methods in the class, there is no need to create objects at the time of operation, using the class method.
3. Static method: Called by the class, no default parameter defines a static method in the class, without passing in your class space, object space, can be used as a normal function.
def static_func ():
class name. Static_func ()
class A:
__name = ' Alex '
def func (self):
print (self, ' in Func ')

@classmethod # class method
def func1 (CLS):
print (CLS, ' in Func1 ')
def login (username,password):
Print ("Login successful")
A.login ("Alex", "123")
The same point: For all methods, it belongs to the class (non-object), so only one copy is saved in memory.
Different points: Different method callers, automatically passing arguments when calling a method
three. Reflection
1. The definition of reflection operates on object-related properties through a string; everything in Python is an object (you can use reflection)
2. A total of four reflections (four functions that can be self-reflective)
GetAttr Gets the value corresponding to this property
hasattr Determine if this attribute is in this object
setattr Setting Properties
delattr To delete an object property
3. Usage:
(1). Instantiate an object
class A:
country = ' China '
def __init__ (self, Name, age): = name
self.age = Age
a1 = A (' Alex ', +)
print (getattr (A1, "Country")) # China
print (getattr (A1, "name")) # Alex
print (hasattr (A1, "Age") # True
SetAttr (A1, "Sex", "male")
print (getattr (A1, "Sex")) # male
delattr (A1, "name")
print (getattr (A1, "name")) # ' A ' object has no attribute ' name '
if Hasattr (A1, "name1"):
print (getattr (A1, ' name1 '))
print ("No ...") # no

(2). Example of a class
class A:
country = ' China '
job = ' Student '

def __init__ (self, Name, age): = name
self.age = Age
def func (self):
print (' in Func ')
content=input ("<<<") # input is a string
if Hasattr (a,content): # equivalent if hasattr (A, "string")
Print (GetAttr (a,content))
Print (GetAttr (A, "func")) # <function A.func at 0x00000145e4f99ae8>
Print (GetAttr (A, "func") (one)) # in Func none (default returns None)
Print (GetAttr (A, "job")) # Student
Print (GetAttr (A, "Job1", "false")) # False
Print (GetAttr (A, "Job1")) # Error type object ' A ' has no attribute ' job1 '

(3). Examples of other modules
Import Old_boy
object=getattr (Old_boy, "B")
Print (GetAttr (object, "add") # 3,4) # The first way to get a static method in a Old_boy file is add
Print (GetAttr (old_boy. B, ' Add ') (3,4) # The second way
Print (GetAttr (old_boy, ' login ') ("Alex", "123") # Call the login function in the Old_boy file

(4). Example of the current module
Import SYS
def login ():
print (in)

def func3 ():
Print (333)
content=input ("<<<")
GetAttr (sys.modules[__name__],content) () #
GetAttr (sys.modules[__name__], "func3") () # 333

Python Full Stack development * 22 Knowledge Point Summary * 180703

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: 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.