【发布时间】:2018-02-08 11:52:18
【问题描述】:
我正在尝试创建自己的类,其行为类似于常规类型,如下所示:
class CustomType:
def __init__(self, content):
self.content = content
def __str__(self):
return self.content
这意味着我可以这样做:
a = CustomType("hi there")
print(a) # 'hi there'
但问题是我必须使用a = CustomType("hi there")。有没有办法让我做类似a = /hi there\ 或类似的解决方案,让我创建自己的语法糖?
谢谢。
【问题讨论】:
-
解析器没有办法知道去寻找那个
-
不。除非你想破解源代码。
标签: python python-3.x class types syntactic-sugar