Hi i wanna change the default text of a class
. like __str__
or __repr__
for object
class Force(SIUnit):
name = "Force"
symbol = "F"
unit = "N"
print(f"F = 1000{Force}")
something like this.
You can define __str__
on metaclass:
class StringClassMeta(type):
def __str__(cls):
return cls.unit
class Force(metaclass=StringClassMeta):
unit = 'N'
print(f'F = 1000 {Force}')
# F = 1000 N
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.