标签:
class Student(object): #object 表示该类从哪个类继承下来的 #·如果没有合适的继承类,就使用object类,这是所有类最终都会继承的类 def __init__(self, name, score): self.name = name self.__score = score def get_score(self): return self.__score def set_score(self, score): self.__score = score def print_score(self): print(‘%s: %s‘ % (self.name, self.__score)) #实例 bart = Student(‘Bart Simpson‘, 59) bart.print_score() print(bart.name) print(bart.get_score()) #print(bart.__score) print(bart._Student__score)
class Animal(object): def run(self): print(‘Animal is running...‘) class Dog(Animal): def run(self): print(‘Dog is running...‘) def eat(self): print(‘Eating meat...‘) d = Dog() d.run() print(isinstance(d, Animal)) print(isinstance(d, Dog))
class Animal(object): def run(self): print(‘Animal is running...‘) class Dog(Animal): def run(self): print(‘Dog is running...‘) def eat(self): print(‘Eating meat...‘) d = Dog() d.run() print(isinstance(d, Animal)) print(isinstance(d, Dog)) #type print(type(123)) print(type(Dog)) print(type(123) == int) import types def fn(): pass print(type(fn) == types.FunctionType) print(type(lambda x : x) == types.LambdaType) #isinstance print(isinstance(d, Dog)) print(isinstance(‘a‘, str)) #是否是某些类型中的一种 print(isinstance([1, 2, 3], (list, tuple))) #dir 获得一个对象的所有属性和方法 print(dir(d)) #getattr()、setattr()以及hasattr() class MyObject(object): def __init__(self): self.x = 9 def power(self): return self.x * self.x obj = MyObject() print(hasattr(obj, ‘x‘)) print(setattr(obj, ‘y‘, 19)) print(getattr(obj, ‘y‘)) print(getattr(obj, ‘z‘, 404)) #如果不存在返回默认值404 print(hasattr(obj, ‘power‘)) print(getattr(obj, ‘power‘))
标签:
原文地址:http://www.cnblogs.com/jzm17173/p/5000203.html