创建语法糖以将变量定义为类实例

The*_*o C 0 python types class syntactic-sugar python-3.x

我正在尝试创建自己的类,其行为类似于常规类型,如下所示:

class CustomType:
    def __init__(self, content):
        self.content = content
    def __str__(self):
        return self.content
Run Code Online (Sandbox Code Playgroud)

这意味着我可以这样做:

a = CustomType("hi there")
print(a)  # 'hi there'
Run Code Online (Sandbox Code Playgroud)

但问题是我必须使用a = CustomType("hi there").有没有办法可以做类似的a = /hi there\或类似的解决方案,让我创建自己的语法糖?

谢谢.

jwo*_*der 5

不,Python不支持创建新语法.