繁体   English   中英

Python OOP - 更改类文本

[英]Python OOP - Change class text

嗨,我想更改class的默认文本。 object__str____repr__

class Force(SIUnit):
    name = "Force"
    symbol = "F"
    unit = "N"


print(f"F = 1000{Force}")

像这样的东西。

您可以在元类上定义__str__

class StringClassMeta(type):
    def __str__(cls):
        return cls.unit

class Force(metaclass=StringClassMeta):
    unit = 'N'

print(f'F = 1000 {Force}')
# F = 1000 N

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM