Function (string function) override overwrite
What is function rewrite
Add the corresponding method in the custom class, so that the instance created by the custom class can operate with the built-in function-
Object to string function
- repr(x) returns an expression string that represents a python object, usually
eval(repr(obj)) == obj
What we get is that we put quotation marks around the object to represent the string - str(x) returns a string (usually for human reading) through a given object
Example:
- repr(x) returns an expression string that represents a python object, usually
s = "I'mTeacher"
print(str(s)) # I'mTeacher, it's a string that people can read
print(repr(s)) # "I'mTeacher" this is an expression (string object) that python can recognize
str(obj) function call method description:
1. The str(obj) function first looks up the obj. \\\\\\\\\
2. If obj. STR does not exist, call the repr method and return the result
3. If the object. Repr example method displays a string in the format of < main, ××××× object at 0xxxxxx >
The following is the rewriting of the object to string function:
class Mynumber():
def __init__(self, val):
self.data = val # Create an instance variable inside each object to bind data
n1 = Mynumber(100)
# STR calls the str method in the parent object
print('str(n1) =', str(n1)) # How to print the custom number 100
print('repr(n1) =', repr(n1))
# The result is
# str(n1) = <__main__.Mynumber object at 0x7fad74af1a20>
# repr(n1) = <__main__.Mynumber object at 0x7fad74af1a20>
print('--------------------Rewrite below str---------------------------')
# This example implies that the overriding method of the object to string function overrides the str method
class Mynumber():
def __init__(self, val):
self.data = val # Create an instance variable inside each object to bind data
def __str__(self):
return "Custom number: %d" % self.data
n1 = Mynumber(100)
# After rewriting, call your own n1.__str__ () method to see the result and overlay the method in object.
print('str(n1) =', str(n1)) # You want to print out the custom number 100
print(n1.__str__())
# str(n1) = custom number: 100
print(n1) # In print, n2 will be converted to string with str(x) and then written to sys.stdout
# Custom number: 100
print('repr(n1) =', repr(n1))
# The call is to the \
# repr(n1) = <__main__.Mynumber object at 0x7fde06b2b978>
print('--------------------Rewrite below repr--------------------------')
# This example implies that the overriding method of the object to string function overrides the method
class Mynumber():
def __init__(self, val):
self.data = val # Create an instance variable inside each object to bind data
def __str__(self):
return "Custom number: %d" % self.data
def __repr__(self):
'''The string returned by this method must be able to represent self Object's expression string'''
return 'MyNumber(%d)' % self.data
n1 = Mynumber(100)
# STR calls the str method in the parent object
# You can override and call your own N1. Str () method to see the result and override the method in the object
print('repr(n1) =', repr(n1))
# repr(n1) = MyNumber(100)