在Python中,我们有类似的事情if,elif,else,break,continue,pass,等.对于一个更好的词想,我会打电话给他们的特别说明.
他们是什么类型的?如果我做的事情就像type(pass)我得到的一样SyntaxError.
我正在尝试创建自己的类,其行为类似于常规类型,如下所示:
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\或类似的解决方案,让我创建自己的语法糖?
谢谢.
在Python 3.5中,给定此字符串:
"rsFooBargrdshtrshFooBargreshyershytreBarFootrhj"
Run Code Online (Sandbox Code Playgroud)
和索引17- 所以,F在第二次出现的开始FooBar- 我如何检查"FooBar"存在?在这种情况下,它应该返回True,而如果我给它索引13它应该返回false.