20170509

1.过滤列表
li=["a","mpilgrim","foo","b","c","d","d"]
[elem for elem in li if len(elem)>1]
['mpilgrim', 'foo']
[elem for elem from li if elem !="b"]

;
File "<input>", line 1
[elem for elem from li if elem !="b"]
^
SyntaxError: invalid syntax
[elem for elem in li if elem !="b"]
['a', 'mpilgrim', 'foo', 'c', 'd', 'd']
[elem for elem in li if li.count(elem)==1]
['a', 'mpilgrim', 'foo', 'b', 'c']
2.安 全 使 用 and-or 技巧
>>> a = ""
>>> b = "second"
>>> (1 and [a] or [b])[0] (1)
''
(1) 由于 [a] 是一个非空列表,所以它决不会为假。即使 a 是 0 或者 '' 或者其
它假值,列表 [a] 也为真,因为它有一个元素。
3. lambda 函数
4.for method in methodList
告诉我们这是一个列表解析。如你所知 methodList 是 object 中所有你关心的方
法的一个列表。所以你正在使用 method 遍历列表

May 10, 2017, 3:48 p.m.

类的继承

1.
class Parent:
parentAttr=100
def __init__(self):
print("调用父类构造函数")
def parentMethod(self):
print("调用父类的方法")
def serAttr(self,attr):
Parent.parentAttr=attr
def getattr(self):
print("父类的属性:",Parent.parentAttr)
class Child(Parent):
def __init__(self):
print("调用子类的构造方法")
def childMethod(self):
print("调用子类的构造方法 child method")
c=Child()
c.childMethod()
c.parentMethod()
c.serAttr(200)
c.getattr()
2.方法重写
.3__repr__
使用repr(obj)的时候,会自动调用__repr__函数,该函数返回对象字符串表达式,
用于重建对象,如果eval(repr(obj))会得到一个对象的拷贝。
#!/usr/local/bin/python
class Study:
def __init__(self,name=None):
self.name = name
def __del__(self):
print "Iamaway,baby!"
def say(self):
print self.name
def __repr__(self):
return "Study('jacky')"
study = Study("zhuzhengjun")
study.say()
print type(repr(Study("zhuzhengjun"))) # str
print type(eval(repr(Study("zhuzhengjun")))) # instance

study = eval(repr(Study("zhuzhengjun")))

study.say()
3.运算符重载
4.字典语法

原文地址:https://www.cnblogs.com/Jt00/p/7154950.html