2020-6-5 13:54:13 | 作者:老铁SEO | 0个评论 | 人浏览
>>>a='hello!world!'
>>>t=a.maketrans('l','a')
>>>a.translate(t)
'heaao!worad!'
>>>ttt=a.maketrans('lh','ab')
>>>a.translate(ttt)
'beaao!worad!'
translate是字符的一一映射.每个字符只要出现都会被替换为对应的字符.
replace是字符串替换,字符串完整出现后被整体替换.replace的两个字符串参数长度可以不同.
Python中的hasattr只能作用于类对象吗?
hasattr同样适用于对象中的方法,因为方法也是属性。
如果对象obj中有get方法,hasattr(obj,'get')将返回True。
jQuery的hasAttr检查,如何查看是否具有一个元素的属性?
<divid="demo"myattr="自定义">jquery判断标签属性是否存在:</div><br><scripttype="text/javascript"><br>$temp=$("#demo").attr("myattra");<br>if(typeof($temp)=="undefined"){//要加typeof()<br>$("#demo").append("没定义该属性:"+$temp);<br>}else{<br>$("#demo").append("定义的属性值为:"+$temp);<br>}<br>//另附上原生js检测方法<br>varobj={name:'jack'};<br>//alert('name'inobj);//-->true<br>//alert('toString'inobj);//-->in能检测到原型链的属性,返回true<br>//alert(obj.hasOwnProperty('name'));//-->true<br>//alert(obj.hasOwnProperty('toString'));//-->原型链上继承过来的属性无法通过hasOwnProperty检测到,返回false<br></script>
linuxgetattr函数什么意思
getattr(obj,"property_nameormethod_name")
可以动态的获取一个对象的属性和方法.
比如,一个命令处理类有很多方法do_createdo_destroydo_listdo_startdo_stop,
那么就可以这样写
defexec(self,command,args):
ifhasattr(self,"do_"+command):
func=getattr(self,"do_"+command)
returnfunc(args)
这样你只要添加这个类的方法,就能直接扩充该类所支持的命令了(python内置的一个命令行解释器模块就是用的这个方式。)