Python中静态方法的实现
作者:老王
Python似乎很讨厌修饰符,没有常见的static语法。其静态方法的实现大致有以下两种方法:
第一种方式(staticmethod):
>>> class Foo:
str = "I'm a static method."
def bar():
print Foo.str
bar = staticmethod(bar)
>>> Foo.bar()
I'm a static method.
第二种方式(classmethod):
>>> class Foo:
str = "I'm a static method."
def bar(cls):
print cls.str
bar = classmethod(bar)
>>> Foo.bar()
I'm a static method.
---------------------------------------------------------------
上面的代码我们还可以写的更简便些:
>>> class Foo:
str = "I'm a static method."
@staticmethod
def bar():
print Foo.str
>>> Foo.bar()
I'm a static method.
或者
>>> class Foo:
str = "I'm a static method."
@classmethod
def bar(cls):
print cls.str
>>> Foo.bar()
I'm a static method.
OK,差不多就是这个样子了。
相关文档:
>>> a="abcd"
>>> ",".join(a)
'a,b,c,d'
>>> "|".join(['a','b','c'])
'a|b|c'
>>> ",".join(('a','b','c'))
'a,b,c'
>>> ",".join({'a':1,'b':2,'c':3})
'a,c,b' ......
eval(str [,globals [,locals ]])函数将字符串str当成有效Python表达式来求值,并返回计算结果。
同样地, exec语句将字符串str当成有效Python代码来执行.提供给exec的代码的名称空间和exec语句的名称空间相同.
最后,execfile(filename [,globals [,locals ]])函数可以用来执行一个文件,看下面的例子:
>>> ev ......
Python字符串操作
python如何判断一个字符串只包含数字字符
python 字符串比较
下面列出了常用的python实现的字符串操作
1.复制字符串
#strcpy(sStr1,sStr2)
sStr1 = 'strcpy'
sStr2 = sStr1
sStr1 = 'strcpy2'
print sStr2
2.连接字符串
#strcat(sStr1,sStr2)
sStr1 = 'strcat'
sStr2 = 'appen ......
使用 Python 分离中文与英文的混合字串
LiYanrui
posted @ 大约 1 年前
in 程序设计
with tags
python
, 614 阅读
这个问题是做 MkIV 预处理程序
时搞定的,就是把一个混合了中英文混合字串分离为英文与中文的子字串,譬如,将 ”我的 English 学的不好
&ld ......