python基础

---恢复内容开始---

tuple 元组/定值表-------字符串是元组   元组元素不可变

list 列表

python子类继承父类的初始化方法:

1.不覆盖,默认执行

2.重写(覆盖),方法: 

  1:ParentClass.__init__(),父类名加上init函数

  2:super(type,cls).__init__() 

super类定义:

class super(object)   
super(type) -> unbound super object  
super(type, obj) -> bound super object; requires isinstance(obj, type)  
super(type, type2) -> bound super object; requires issubclass(type2, type)  
Typical use to call a cooperative superclass method:  
class C(B):  
    def meth(self, arg):  
        super(C, self).meth(arg) 

建议用super方法

  

>>> getattr(s, 'name')  # 获取name属性
'Bob'

>>> setattr(s, 'name', 'Adam')  # 设置新的name属性

>>> s.name
'Adam'

>>> getattr(s, 'age')  # 获取age属性,但是属性不存在,报错:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Student' object has no attribute 'age'

>>> getattr(s, 'age', 20)  # 获取age属性,如果属性不存在,就返回默认值20:
20
for k, v in kw.iteritems():
            setattr(self, k, v)

python字典的items方法作用:是可以将字典中的所有项,以列表方式返回。如果对字典项的概念不理解,可以查看Python映射类型字典基础知识一文。因为字典是无序的,所以用items方法返回字典的所有项,也是没有顺序的。
python字典的iteritems方法作用:与items方法相比作用大致相同,只是它的返回值不是列表,而是一个迭代器。

 

posted @ 2017-04-06 12:05  foreverwith  阅读(112)  评论(0)    收藏  举报