面向对象
type类型
import types
def fun():
pass
print(type(fun) == types.FunctionType)
print(type(abs) == types.BuiltinFunctionType)
print(type(lambda x: x * x) == types.LambdaType)
print(type((x for x in range(0, 10))) == types.GeneratorType)
对象
class Student(object):
"""
学生类
"""
def __init__(self, name, age, address):
self.name = name
self.age = age
self.__address = address
def print_name(self):
print(self.name)
def __good(self):
print("good good study")
def get_address(self):
return self.__address
stu = Student("张三", 18, "重庆渝北区")
name = stu.name
age = stu.age
print(name, age)
stu.gender = "man"
print(stu.gender)
stu.print_name()
print(stu.__class__)
print(stu.__doc__)
print(stu.get_address())
继承、多态
class Car(object):
def run(self):
print("---Car跑---")
def whistle(self):
print("滴!滴!滴!")
class Roadster(Car):
def run(self):
print("---Roadster飞速的跑---")
def print_person(self):
print("载4人")
class Bus(Car):
def run(self):
print("---Bus一般的跑---")
def print_person(self):
print("载20人")
car = Car()
car.run()
car.whistle()
roadster = Roadster()
roadster.run()
roadster.print_person()
roadster.whistle()
bus = Bus()
bus.run()
bus.print_person()
bus.whistle()
print(type(bus))
print(isinstance(bus, Car))
def run_twice(car):
car.run()
car.run()
run_twice(roadster)
print(dir(bus))
class Animal(object):
def walk(self):
print("动物走路")
class Pet(object):
def like(self):
print("让人喜欢")
class Dog(Animal, Pet):
def call(self):
print("汪汪汪")
lily = Dog()
lily.walk()
lily.like()
lily.call()
函数操作对象的状态
class Person(object):
def __init__(self, age):
self.age = age
def add_age(self, num):
self.age += num
xiaoming = Person(18)
setattr(xiaoming, "x", 10)
print(getattr(xiaoming, "x"))
print(getattr(xiaoming, "q", 404))
print(hasattr(xiaoming, "add_age"))
xiaoming.y = 20
print(xiaoming.y)
print(dir(xiaoming))
def hello():
print("你好!")
setattr(xiaoming, "ni", hello)
nihao = getattr(xiaoming, "ni")
nihao()
print(dir(xiaoming))
函数内的方法
class Student(object):
__slots__ = ("address", "school")
tom = Student()
tom.address = "重庆渝北区"
tom.school = "江北区小小小学"
print(tom.address, tom.school)
class Fibo(object):
"""
斐波那契数列
"""
def __init__(self):
self.a, self.b = 0, 1
def __iter__(self):
return self
def __next__(self):
self.a, self.b = self.b, self.a + self.b
if self.a > 30:
raise StopIteration()
return self.a
for i in Fibo():
print(i)
class Fibo2(object):
def __getitem__(self, item):
a, b = 1, 1
for i in range(item):
a, b = b, a + b
return a
numbers = Fibo2()
print(numbers[5])
class Student(object):
def __init__(self, age):
self.age = age
def __call__(self, *args, **kwargs):
print("__call__ age=" + str(self.age))
tom = Student(18)
tom()
class Student(object):
@property
def get_score(self):
return self.__score
@get_score.setter
def set_score(self, score):
if not isinstance(score, (int, float)):
raise TypeError("score必须是int或float")
if score < 0 or score > 150:
raise ValueError("score值必须在0-150中")
self.__score = score
@property
def get_birth(self):
return self.__birth
@get_birth.setter
def set_birth(self, value):
self.__birth = value
@property
def age(self):
return 2017 - self.__birth
jerry = Student()
jerry.set_score = 100
score = jerry.get_score
print(score)
jerry.set_birth = 1994
print(jerry.age)
枚举
from enum import Enum, unique
Month = Enum("Month", ("一月", "二月", "三月", "四月"))
for i, name in Month._member_map_.items():
print(i, name, name.value)
@unique # 加上@unique后,值不能重复
class WeekDay(Enum):
Sun = 0
Mon = 1
Tue = 2
Wed = 3
day = WeekDay.Mon
print(day)
print(WeekDay.Tue)
print(WeekDay(1))
print(WeekDay.Tue.value)