# encoding: utf-8
import functools
import collections
# ************************************************属性相关************************************************
# 类的属性和对象的属性都存在各自的dict中
# 对象的dict可以直接被修改
# 类的dict为只读,默认无法修改
class Person:
name = "admin"
age = 18
p = Person()
p.name = "marry"
p.age = 20
print(Person.age, Person.name)
print(p.age, p.name)
print(Person.__dict__)
print(p.__dict__)
p.__dict__ = {"name": "jack", "age": 23}
# ************************************************
# __slots__ 限制对象属性的添加
class Person:
__slots__ = ["name", "age"]
pass
# ************************************************方法相关************************************************
class Person:
# 实例方法
def eat(self, food):
print(food)
# 类方法
@classmethod
def class_fun(cls, food):
print(food)
# 静态方法
@staticmethod
def static_fun(food):
print(food)
# ************************************************
# 实例方法可以访问实例属性和类属性
# 类方法和静态方法只能访问类属性
class Person():
age = 18
# 实例方法
def eat(self, food):
print(self.age)
print(self.name)
print(food)
# 类方法
@classmethod
def class_fun(cls, food):
print(cls.age)
print(food)
# 静态方法
@staticmethod
def static_fun(food):
print(Person.age)
print(food)
p = Person()
p.name = "admin"
p.eat("appale")
p.class_fun("appale")
p.static_fun("appale")
# ************************************************元类************************************************
# <class 'type'> 元类 所有类对象是由元类创建的
print(str.__class__)
print(Person.__class__)
print(type.__class__)
# ************************************************
# 类的创建流程
#
#
# 1:检测类对象中是否有明确metaclass属性
# 2:检测父类中是否有明确metaclass属性
# 3:检测模块中是否有明确metaclass属性
# 4:通过内置的type这个元类,来创建这个类对象
# ************************************************属性访问权限划分************************************************
# 共有属性(x)访问权限:
# 类内部访问
# 子类内部访问
# 模块内其他位置访问
# 跨模块访问
#
#
# 受保护的属性(_x)访问权限:
# 类内部访问
# 子类内部访问
# 模块内其他位置访问(有警告)
# 跨模块访问import形式导入(有警告)
# 跨模块访问from import (__all__)
#
#
# 私有属性访问权限(__x):
# 类内部访问
# 跨模块访问from import (__all__)
# ************************************************
# python并没有真正的私有化支持,但是可以使用下划线完成伪私有的效果
# 私有属性的实现机制:名字重整:重改_x为_类名__x
class Animal:
__name = "cat"
pass
print(Animal._Animal__name)
# ************************************************只读属性************************************************
# python3中,如果直接定义一个类,会隐式地继承object,默认是一个新式类
class Person:
def __init__(self):
self.__age = 20
def get_age(self):
return self.__age
age = property(get_age)
p = Person()
print(p.age)
# ************************************************私有化方法************************************************
class Person:
def __fun(self):
return "python"
# ************************************************内置特殊方法************************************************
# ************************************************
# __init__ __str__ __repr__
class Animal:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return "姓名是%s,年龄是%d" % (self.name, self.age)
def __repr__(self):
return "ssssss"
def __call__(self, *args, **kwargs):
print(args, kwargs)
a = Animal("cat", 2)
print(a)
print(repr(a))
a("java", "python", height=170)
# ************************************************
# __call__
class PenFactory:
def __init__(self, pen_type):
self.pen_type = pen_type
def __call__(self, pen_color):
print("笔的类型是%s,笔的颜色是%s" % (self.pen_type, pen_color))
pen = PenFactory("钢笔")
pen("黄色")
pen("蓝色")
pen("红色")
pen = PenFactory("铅笔")
pen("黄色")
pen("蓝色")
pen("红色")
# ************************************************
# 索引操作:__setitem__ __getitem__ __delitem__
class Person:
def __init__(self):
self.cache = {}
def __setitem__(self, key, value):
self.cache[key] = value
def __getitem__(self, item):
return self.cache[item]
def __delitem__(self, key):
del self.cache[key]
p = Person()
p["name"] = "admin"
p["password"] = "password"
del p["password"]
p["name"] = "python"
print(p["name"])
print(p.cache)
# ************************************************
# 比较操作
@functools.total_ordering
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __lt__(self, other):
return self.age < other.age
def __eq__(self, other):
return self.age == other.age
def __ne__(self, other):
return self.age != other.age
def __bool__(self):
return self.age >= 18
print(Person.__dict__)
# ************************************************
# __bool__
class Person:
def __init__(self, age):
self.age = age
def __bool__(self):
return self.age >= 18
p = Person(20)
if p:
print("OK")
# ************************************************
# 遍历操作
class Person:
def __init__(self):
self.age = 0
def __iter__(self):
# 迭代器的复用
self.age = 0
return self
def __next__(self):
self.age += 1
if self.age > 6:
raise StopIteration("stop")
return self.age
p = Person()
for i in p:
print(i)
# 迭代器的复用
for i in p:
print(i)
# 判定是否为迭代器(迭代器必须要实现 __iter__ 和 __next__ 方法)
print(isinstance(p, collections.Iterator))
# 判定是否为可迭代对象(迭代器必须要实现 __iter__ 方法)
print(isinstance(p, collections.Iterable))
# 可迭代对象一定可以实现for in遍历,但能实现for in遍历(__getitem__)不一定是可迭代对象
class Person:
def __init__(self):
self.age = 0
def __getitem__(self, item):
self.age += 1
if self.age > 6:
raise StopIteration("stop")
return self.age
p = Person()
for i in p:
print(i)