小编The*_*o C的帖子

Python中"特殊"运算符/指令的类型是什么?

在Python中,我们有类似的事情if,elif,else,break,continue,pass,等.对于一个更好的词想,我会打电话给他们的特别说明.

他们是什么类型的?如果我做的事情就像type(pass)我得到的一样SyntaxError.

python

1
推荐指数
1
解决办法
81
查看次数

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

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

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 types class syntactic-sugar python-3.x

0
推荐指数
1
解决办法
139
查看次数

检查字符串是否包含索引处的子字符串

在Python 3.5中,给定此字符串:

"rsFooBargrdshtrshFooBargreshyershytreBarFootrhj"
Run Code Online (Sandbox Code Playgroud)

和索引17- 所以,F在第二次出现的开始FooBar- 我如何检查"FooBar"存在?在这种情况下,它应该返回True,而如果我给它索引13它应该返回false.

python string substring

0
推荐指数
1
解决办法
564
查看次数

标签 统计

python ×3

class ×1

python-3.x ×1

string ×1

substring ×1

syntactic-sugar ×1

types ×1