day22
2018-04-16 本文已影响4人
两分与桥
eval 模块转换dict, list, tuple
dict = {'name':'gg', 'age':18}
lists = [1,2,3,4,5,6,[7,8]]
tuples = (1,2,3,4,5,'a','c')
print(eval(str(dict)))
print(eval(str(lists)))
print(eval(str(tuples)))
json
loads and down
静态方法 @property
class tests:
def __init__(self, x, y):
self.x = x
self.y = y
@property
def sum(self):
print('x*y = %s' %(self.x+self.y))
test = tests(5,8)
print(test.x)
print(test.y)
test.sum
输出结果:
5
8
x*y = 13
@类方法 @classmethod
class tests:
tag = 1
def __init__(self, x, y):
self.x = x
self.y = y
@property
def sum(self):
print('x*y = %s' %(self.x+self.y))
@classmethod
def ggg(cls):
print(cls)
print('==',cls.tag)
test = tests(5,8)
print(test.x)
test.sum
print("*************************")
tests.ggg()
输出结果:
5
x*y = 13
*************************
<class '__main__.tests'>
== 1
静态属性
class tests:
tag = 1
def __init__(self, x, y):
self.x = x
self.y = y
@property
def sum(self):
print('x*y = %s' %(self.x+self.y))
@classmethod
def ggg(cls,z): //类方法传参
print(cls)
print('==',cls.tag, z)
@staticmethod
def xxx(a,b):
print(a,b)
test = tests(5,8)
print(test.x)
test.sum
print("*************************")
tests.ggg(22)
print("*************************")
tests.xxx(2,3)
输出结果:
5
x*y = 13
*************************
<class '__main__.tests'>
== 1 22
*************************
2 3
组合,先实例化 Teacher,再把实例化的 t 传入实例化 School 中,实现组合
class School:
def __init__(self, local, teacher_name):
self.local = local
self.teacher_name = teacher_name
class Teacher:
def __init__(self, name):
self.name = name
t = Teacher('gg') //将实例化的 t 传入 s 中
s = School('Beijing', t)
print(t.__dict__)
print(s.__dict__)
print(s.teacher_name.name)
输出结果:
{'name': 'gg'}
{'local': 'Beijing', 'teacher_name': <__main__.Teacher object at x000001C089ECAB38>}
gg
继承,子类继承父类
class man:
def __init__(self, name, gender):
self.name = name
self.gender = gender
def head(self):
print('this is head')
def others(self):
print('this is others')
class student(man): #继承父类
# 子类可以再定义,定义重复会使用子类方法,如 init
def __init__(self, id, name, gender):
self.id = id
self.name = name
self.gender = gender
s = student('16040', 'libai', 'none')
print(s.__dict__)
s.head()
输出结果:
{'id': '16040', 'name': 'libai', 'gender': 'none'}
this is head
接口继承,在子类中必须定义某些方法,如 others
import abc
class man(metaclass=abc.ABCMeta):
def __init__(self, name, gender):
self.name = name
self.gender = gender
def head(self):
print('this is head')
@abc.abstractmethod #要求必须在子类中定义others方法,没定义不能实例化
def others(self):
print('this is others')
class student(man): #继承父类
# 子类可以再定义,定义重复会使用子类方法
def __init__(self, id, name, gender):
self.id = id
self.name = name
self.gender = gender
def others(self): #必须定义others
print('son others')
s = student('16040', 'libai', 'none')
print(s.__dict__)
s.others()
输出结果:
{'id': '16040', 'name': 'libai', 'gender': 'none'}
son others
继承顺序,深度优先与广度优先,python3 都是新式类,python2 分为新式类和经典类
可以看:https://blog.csdn.net/oxiaoxio/article/details/50463484
子类调用父类方法
class man():
def __init__(self, name, gender):
self.name = name
self.gender = gender
def head(self):
print('this is head')
def others(self):
print('this is others')
class student(man):
def __init__(self, id, name, gender):
man.__init__(self, name, gender) #子类使用父类方法,必须传入self
self.id = id
def others(self):
man.others(self) #使用父类方法
print('son others')
s = student('16040', 'libai', 'none')
print(s.__dict__)
s.others()
输出结果:
{'name': 'libai', 'gender': 'none', 'id': '16040'}
this is others
son others
用 super 调用父类方法
class man():
def __init__(self, name, gender):
self.name = name
self.gender = gender
def others(self):
print('this is others')
class student(man):
def __init__(self, id, name, gender):
super().__init__(name, gender) #用super调用父类方法
# 类似于 super(student, self).__init__(name, gender)
self.id = id
def others(self):
super().others() #super
print('son others')
s = student('16040', 'libai', 'none')
print(s.__dict__)
s.others()
输出结果:
{'name': 'libai', 'gender': 'none', 'id': '16040'}
this is others
son others