Magic function - python-- object-oriented


## Magic function built-in Python function can be rewritten
in Python, starts with a double underline, double underline ending is a member of the system definition. We can be overridden in a custom class to change its behavior.

__str__ function: converts a string object (human friendly)
__repr__ function: converts a string object (interpreter recognizable)


"" "
Object -> str
Exercise: demo03.py
" ""


class StudentModel:
def __init__(self, name="", age=0, score=0.0):
self.name = name
self.age = age
self.score = score

# Object -> str: to be friendly (no format restrictions)
DEF __str __ (Self):
return "I'm% s, age:% d, score is% .1f"% (self.name, self.age , self.score)

Object # -> str: interpreter may identify (python format must comply with the syntax)
DEF __repr __ (Self):
return 'StudentModel ( "% S",% D, 1F%.)'% (The self.name, self.age , self.score)


s01 = StudentModel ( "Monkey", 27, 100)
# 1. Object -> str (viewed)
. Print (S01) # <__ __ StudentModel main AT 0x7ff62a30d400 Object>

msg = str(s01) # s01.__str__()
print(msg)

#
# 2. the eval function: as a python string code execution
Print (the eval ( "*. 5. 1 + 2"))
# Re = the eval ( 'StudentModel ( "Monkey", 27,100)')

Object clone # + the repr the eval
# 3. Objects -> str (see interpreter)
Re = the eval (the repr (S01)) .__ S01 the repr __ # ()
# Re = S01
s01.name = "Monkey"
Print (re.name )

print(re)

 

Operator
"" "
Operator Overloading (rewrite)
Applicability: you want the object to create a custom class, using python operators.
Key: __ eq__ __str__ __repr__
" ""
# 17:00
class vector1:
DEF __init __ (Self, the X-0 = ):
self.x = X

__ __str DEF (Self):
return "component of the vector is:" + str (self.x)

# Arithmetic operators: + additional types of self-
DEF __add __ (Self, OTHER):
IF type (OTHER) == vector1:
return vector1 (self.x + other.x)
the else:
return vector1 (self.x + OTHER)

# Reverse arithmetic operators: other types of its own type +
DEF __radd __ (Self, OTHER):
return the Add Self .__ __ (OTHER)

# Compound operators: If not present, the default call __add__, returns a new object.
# If there are, they tend to return to their own objects in __iadd__ in.
DEF __iadd __ (Self, OTHER):
IF of the type (OTHER) == vector1:
+ = other.x self.x
the else:
self.x + = OTHER
return Self

# Comparison operators: If not present, the default address to use for comparison.
# Even if the same respective data objects, is determined also to false.
DEF __eq __ (Self, OTHER):
return self.x == other.x


v01 = Vector1(20)
v02 = Vector1(20)

Arithmetic operators # 1.
Print (V01 + V02) .__ the Add __ # V01 (V02)
# 1 Exercise: The other two choose their own operator overloading implemented.

# 2. Reverse arithmetic operators
# Print (V01 + 20.5) # V01 .__ the Add __ (20.5)
Print (20.5 + V01) # V01 .__ RADD __ (20.5)
Print (V01 + 20.6)
# Exercise 2: choose other 2 reverse arithmetic overloading achieved.

3. Composite # arithmetic operators
Print (ID (V01))
V01 + = 2
Print (ID (V01))
Print (V01)

List01 = # [l, 2,3]
# Print (ID (list01))
# = list01 + [. 4]
# Print (ID (list01))
# Print (list01)
# Exercise 3: The other two composite choose their own operator overloading achieve.

# 4. Comparison operators
Print (== V01 V02)
# 4 Exercise: self-select to achieve the other two comparison operator overloading.

Guess you like

Origin www.cnblogs.com/chenlulu1122/p/11922124.html