全网最详细的Python学习笔记,值得收藏

我总结了python已下点:

面向对象

  • 类和对象的创建
  • 属相相关
  • 方法相关
  • 元类
  • 内置的特殊属性
  • 内置的特殊方法
    PS注意:不管你是为了Python就业还是兴趣爱好,记住:项目开发经验永远是核心,如果你缺新项目练习或者没有python精讲教程,可以去小编的Python交流.裙 :七衣衣九七七巴而五(数字的谐音)转换下可以找到了,里面很多新教程项目,还可以跟老司机交流讨教!

类和对象的创建

# 经典类 没有继承 object的类
# 新式类 继承了 object的类

class Money: # 2.x中默认是经典类,3.x中是新式类 pass class Money(object): # 兼容的一种写法 pass # Money既是类的__name__属性名,又是一个引用该类的变量 print(Money.__name__) # Money xxx = Money print(xxx.__name__) # Money 
对象
one = Money()
print(one) # <__main__.Money object at 0x000001555E9534A8> print(one.__class__) # <class '__main__.Money'> 

属性相关

对象属性
class Person:
    pass

p = Person() # 给 p对象增加属性, 所有的属性好像是以字典的形式组织的 p.age = 18 print(p.age) # 18 print(p.__dict__) # {'age': 18} print(p.sex) # AttributeError: 'Person' object has no attribute 'sex' # 删除p对象的属性 del p.age print(p.age) # AttributeError: 'Person' object has no attribute 'age' 
类属性
class Money:
    num = 666 count = 1 type = "rmb" print(Money.num) # 666 # 对象查找属性,先到对象自身去找,若未找到,根据 __class__找到对应的类,然后去类中查找 one = Money() print(one.count) # 1 # 不能通过对象去 修改/删除 对应类的属性 one.num = 555 print(Money.num) # 666 print(one.num) # 555 # 类属性会被各个对象共享 two = Money() print(one.num, two.num) # 666 666 Money.num = 555 print(one.num, two.num) # 555 555 
限制对象的属性添加
# 类中的 __slots__属性定义了对象可以添加的所有属性

class Person:
    __slots__ = ["age"] # 只允许添加一个 age属性 p1 = Person() p1.age = 1 p1.num = 2 # AttributeError: 'Person' object has no attribute 'num' 
私有化属性
  • Python没有真正的私有化支持,只能用给变量添加下划线来实现伪私有;通过名字重整机制

  • 属性的访问范围:类的内部-->子类内部-->模块内的其他位置-->其他模块

  • 公有属性 x

    • [x] 类的内部
    • [x] 子类内部
    • [x] 模块内的其他位置
    • [x] 子类内部
  • 受保护属性 _x

    • [x] 类的内部
    • [x] 子类内部
    • [x] 模块内的其他位置(但不推荐)
    • [x] 子类内部(from ... import xxx 不可以访问,要指明all变量)
  • 私有属性 __x

    • [x] 类的内部
    • [ ] 子类内部
    • [ ] 模块内的其他位置
    • [ ] 子类内部(同_x)
  • 保护数据案例

class Person:
    def __init__(self): self.__age = 18 def set_age(self, age): # 错误数据的过滤 if isinstance(age, int) and 0 < age < 150: self.__age = age else: print("Wrong age value") def get_age(): return self.__age p = Person() print(p.get_age()) # 18 p.set_age(22) print(p.get_age()) # 22 
  • 只读属性
# 1. 属性私有化 + 属性化 get()方法
class Person(object): def __init__(self): self.__age = 18 # 可以以使用属性的方式来使用方法 @property def age(self): return self.__age p = Person() print(p.age) # 18 p.age = 666 # Attribute Error: can't set attribute # 2. 通过底层的一些函数 class Person: # 通过 属性 = 值 的方式来给一个对象增加属性时,底层都会调用这个方法,构成键值对,存储在 __dict__字典中 # 可以考虑重写底层的这个函数,达到只读属性的目的 def __setattr__(self, key, value): if key == "age" and key in __dict__: print("read only attribute") else: self.__dict__[key] = value 

方法相关

方法的划分
  • 实例方法
  • 类方法
  • 静态方法
class Person:

    def instance_fun(self): # self: 调用对象的本身,调用时不用写,解释器会传参 print("instance method", self) @classmethod def class_fun(cls): # cls: 类本身 print("class method", cls) @staticmethod def static_fun(): print("static method") 
  • 所有的方法都存储在类中,实例中不存储方法
  • 类方法和静态方法无法访问实例属性
方法的私有化
  • 和变量的私有化思想差不多
class Person:
    __age = 18 def __run(self): # 只能在该类中被调用 print("running...") 

元类

  • 创建类对象的类(类也是一个对象)
a, s = 8, "123" print(a.__class__, s.__class__) # <class 'int'> <class 'str'> print(int.__class__, str.__class__) # <class 'type'> <class 'type'> 
  • type是元类。

     
    image
  • 通 type 元类来创建类,动态创建。也可以用__metaclass__来指明元类,进行类的创建。

    • 检测类对象中是否有metaclass属性
    • 检测父类中是否有metaclass属性
    • 检测模块中是否有metaclass属性
    • 通过内置的type来创建类
def run(self): print("run...") Dog = type("Dog", (), {"count": 0, "run": run}) print(Dog) # <class '__nain__.Dog'> d = Dog() print(d.count) # 0 print(d.run()) # run... 

内置的特殊属性

 
image

内置的特殊方法

  • 信息格式化操作
    calss Person:
        def __init__(self, n, a): self.name = n self.age = a # 面向用户 def __str__(self): return "name: %s, age: %d" % (self.name, self.age) # 面向开发人员 def __repr__(self): # todo # 一般默认给出对象的类型及地址信息等 # 打印或进行格式转换时,先调用 __str__()函数,若未实现,再调用 __repr__()函数 p = Person("Rity", 18) print(p) # name: Rity, age: 18 res = str(p) print(res) # name: Rity, age: 18 print(repr(p)) # <__main__.Person object at 0x000001A869BEB470> 
  • 调用操作
    # 使得一个对象可以像函数一样被调用
    class PenFactory:
        def __init__(self, type): self.type = type def __call__(self, color): print("get a new %s, its color is %s" % (self.type, color)) pencil = PenFactory("pencil") pen = PenFactory("pen") # 一下两种使用方式会调用 __call__()函数 pencil("red") # get a new pencil, ites color is red pencil("blue") # get a new pencil, ites color is blue pen("black") # get a new pen, ites color is black 
  • 索引操作
    class Person:
        def __init__(self): self.cache = {} def __setitem__(self, key, value): self.cache[key] = value def __getitem__(self, key): return self.cache[key] def __delitem__(self, key): del self.cache[key] p = Person() p["name"] = "Rity" ... 
  • 比较操作
    # 使得自己定义的类可以按照一定的规则进行比较
    import functools
    
    @functools.total_ordering
    class A: def __init__(self, age, height): self.age = age self.height = height def __eq__(self, other): # == return self.age == other.age def __lt__(self, ohter): # < return self.age < other.age a1, a2 = A(18, 170), A(19, 178) # 因为逻辑具有相反性,当使用 > 时,首先会查找 __gt__()函数,若未定义,将参数交换后调用 __lt()__方法 # 由 == 和 < 可以组合出其他的所有比价逻辑,使用装饰器可以自动生成其他逻辑对应的函数,简化代码 print(a1 < a2) # True print(a2 > a1) # True print(a1 >= a2) # False 
最后提醒大家不管你是为了Python就业还是兴趣爱好,记住:项目开发经验永远是核心,如果你缺新项目练习或者没有python精讲教程,可以去小编的Python交流.裙 :七衣衣九七七巴而五(数字的谐音)转换下可以找到了,里面很多新教程项目,还可以跟老司机交流讨教!

本文的文字及图片来源于网络加上自己的想法,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。

猜你喜欢

转载自www.cnblogs.com/chengxuyuanaa/p/12586639.html