forked from Elvin-Ma/pytorch_guide
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
elvin
committed
Jul 28, 2023
1 parent
7aa1fbe
commit 5230eca
Showing
3 changed files
with
256 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# 魔术方法汇总 | ||
|
||
## 基本魔术方法 | ||
_new__(cls[, ...]) 1. __new__ 是在一个对象实例化的时候所调用的第一个方法 | ||
__init__(self[, ...]) 构造器,当一个实例被创建的时候调用的初始化方法 | ||
__del__(self) 析构器,当一个实例被销毁的时候调用的方法 | ||
__call__(self[, args...]) 允许一个类的实例像函数一样被调用:x(a, b) 调用 x.__call__(a, b) | ||
__len__(self) 定义当被 len() 调用时的行为 | ||
__repr__(self) 定义当被 repr() 调用时的行为 | ||
__str__(self) 定义当被 str() 调用时的行为 | ||
__bytes__(self) 定义当被 bytes() 调用时的行为 | ||
__hash__(self) 定义当被 hash() 调用时的行为 | ||
__bool__(self) 定义当被 bool() 调用时的行为,应该返回 True 或 False | ||
__format__(self, format_spec) 定义当被 format() 调用时的行为 | ||
|
||
## 属性相关魔术方法 | ||
__getattr__(self, name) 定义当用户试图获取一个不存在的属性时的行为 | ||
__getattribute__(self, name) 定义当该类的属性被访问时的行为 | ||
__setattr__(self, name, value) 定义当一个属性被设置时的行为 | ||
__delattr__(self, name) 定义当一个属性被删除时的行为 | ||
__dir__(self) 定义当 dir() 被调用时的行为 | ||
__get__(self, instance, owner) 定义当描述符的值被取得时的行为 | ||
__set__(self, instance, value) 定义当描述符的值被改变时的行为 | ||
__delete__(self, instance) 定义当描述符的值被删除时的行为 | ||
|
||
## 比较操作符 | ||
__lt__(self, other) 定义小于号的行为:x < y 调用 x.__lt__(y) | ||
__le__(self, other) 定义小于等于号的行为:x <= y 调用 x.__le__(y) | ||
__eq__(self, other) 定义等于号的行为:x == y 调用 x.__eq__(y) | ||
__ne__(self, other) 定义不等号的行为:x != y 调用 x.__ne__(y) | ||
__gt__(self, other) 定义大于号的行为:x > y 调用 x.__gt__(y) | ||
__ge__(self, other) 定义大于等于号的行为:x >= y 调用 x.__ge__(y) | ||
|
||
## 算术运算符 | ||
__add__(self, other) 定义加法的行为:+ | ||
__sub__(self, other) 定义减法的行为:- | ||
__mul__(self, other) 定义乘法的行为:* | ||
__truediv__(self, other) 定义真除法的行为:/ | ||
__floordiv__(self, other) 定义整数除法的行为:// | ||
__mod__(self, other) 定义取模算法的行为:% | ||
__divmod__(self, other) 定义当被 divmod() 调用时的行为 | ||
__pow__(self, other[, modulo]) 定义当被 power() 调用或 ** 运算时的行为 | ||
__lshift__(self, other) 定义按位左移位的行为:<< | ||
__rshift__(self, other) 定义按位右移位的行为:>> | ||
__and__(self, other) 定义按位与操作的行为:& | ||
__xor__(self, other) 定义按位异或操作的行为:^ | ||
__or__(self, other) 定义按位或操作的行为:| | ||
|
||
## 反运算 | ||
__radd__(self, other) (与上方相同,当左操作数不支持相应的操作时被调用) | ||
__rsub__(self, other) (与上方相同,当左操作数不支持相应的操作时被调用) | ||
__rmul__(self, other) (与上方相同,当左操作数不支持相应的操作时被调用) | ||
__rtruediv__(self, other) (与上方相同,当左操作数不支持相应的操作时被调用) | ||
__rfloordiv__(self, other) (与上方相同,当左操作数不支持相应的操作时被调用) | ||
__rmod__(self, other) (与上方相同,当左操作数不支持相应的操作时被调用) | ||
__rdivmod__(self, other) (与上方相同,当左操作数不支持相应的操作时被调用) | ||
__rpow__(self, other) (与上方相同,当左操作数不支持相应的操作时被调用) | ||
__rlshift__(self, other) (与上方相同,当左操作数不支持相应的操作时被调用) | ||
__rrshift__(self, other) (与上方相同,当左操作数不支持相应的操作时被调用) | ||
__rand__(self, other) (与上方相同,当左操作数不支持相应的操作时被调用) | ||
__rxor__(self, other) (与上方相同,当左操作数不支持相应的操作时被调用) | ||
__ror__(self, other) (与上方相同,当左操作数不支持相应的操作时被调用) | ||
|
||
## 增量赋值运算符 | ||
__iadd__(self, other) 定义赋值加法的行为:+= | ||
__isub__(self, other) 定义赋值减法的行为:-= | ||
__imul__(self, other) 定义赋值乘法的行为:*= | ||
__itruediv__(self, other) 定义赋值真除法的行为:/= | ||
__ifloordiv__(self, other) 定义赋值整数除法的行为://= | ||
__imod__(self, other) 定义赋值取模算法的行为:%= | ||
__ipow__(self, other[, modulo]) 定义赋值幂运算的行为:**= | ||
__ilshift__(self, other) 定义赋值按位左移位的行为:<<= | ||
__irshift__(self, other) 定义赋值按位右移位的行为:>>= | ||
__iand__(self, other) 定义赋值按位与操作的行为:&= | ||
__ixor__(self, other) 定义赋值按位异或操作的行为:^= | ||
__ior__(self, other) 定义赋值按位或操作的行为:|= | ||
|
||
## 一元操作符 | ||
__pos__(self) 定义正号的行为:+x | ||
__neg__(self) 定义负号的行为:-x | ||
__abs__(self) 定义当被 abs() 调用时的行为 | ||
__invert__(self) 定义按位求反的行为:~x | ||
|
||
## 类型转换 | ||
__complex__(self) 定义当被 complex() 调用时的行为(需要返回恰当的值) | ||
__int__(self) 定义当被 int() 调用时的行为(需要返回恰当的值) | ||
__float__(self) 定义当被 float() 调用时的行为(需要返回恰当的值) | ||
__round__(self[, n]) 定义当被 round() 调用时的行为(需要返回恰当的值) | ||
__index__(self) | ||
1. 当对象是被应用在切片表达式中时,实现整形强制转换 | ||
2. 如果你定义了一个可能在切片时用到的定制的数值型,你应该定义 __index__ | ||
3. 如果 __index__ 被定义,则 __int__ 也需要被定义,且返回相同的值 | ||
|
||
## 上下文管理(with 语句) | ||
__enter__(self) | ||
1. 定义当使用 with 语句时的初始化行为 | ||
2. __enter__ 的返回值被 with 语句的目标或者 as 后的名字绑定 | ||
__exit__(self, exc_type, exc_value, traceback) | ||
1. 定义当一个代码块被执行或者终止后上下文管理器应该做什么 | ||
2. 一般被用来处理异常,清除工作或者做一些代码块执行完毕之后的日常工作 | ||
|
||
## 容器类型 | ||
__len__(self) 定义当被 len() 调用时的行为(返回容器中元素的个数) | ||
__getitem__(self, key) 定义获取容器中指定元素的行为,相当于 self[key] | ||
__setitem__(self, key, value) 定义设置容器中指定元素的行为,相当于 self[key] = value | ||
__delitem__(self, key) 定义删除容器中指定元素的行为,相当于 del self[key] | ||
__iter__(self) 定义当迭代容器中的元素的行为 | ||
__reversed__(self) 定义当被 reversed() 调用时的行为 | ||
__contains__(self, item) 定义当使用成员测试运算符(in 或 not in)时的行为 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
""" 属性访问顺序 | ||
__getattribute__ | ||
数据描述符 : 从覆盖性描述符中获取(实现了__set__方法) | ||
当前对象的属性 __dict__ 中获取 | ||
类的属性 __dict__ 中获取 | ||
非数据描述符 调用非覆盖型描述符(没有实现__set__方法) | ||
父类的属性 从实例父类的 __dict__ 字典获取 | ||
__getattr__ | ||
""" | ||
|
||
class User: | ||
def __init__(self, name, sex): | ||
self.name = name | ||
self.sex = sex | ||
|
||
def __getattribute__(self, item): | ||
''' | ||
在访问对象成员属性的时候触发,无论是否存在。 | ||
返回值千万不能用self.name,这样会无限递归下去 | ||
''' | ||
# return 6 | ||
return object.__getattribute__(self, item) | ||
|
||
def getattribute_demo(): | ||
user1 = User("monica", "girl") | ||
print(user1.name) | ||
|
||
class A(object): | ||
def __init__(self, x): | ||
self.x = x | ||
|
||
def hello(self): | ||
return 'hello func' | ||
|
||
def __getattr__(self, item): | ||
''' be called if __getattribute__ return none''' | ||
print('in __getattr__') | ||
return super(A, self).__getattribute__(item) | ||
|
||
def __getattribute__(self, item): | ||
''' must call ''' | ||
print('in __getattribute__') | ||
return super(A, self).__getattribute__(item) | ||
|
||
def getattr_demo(): | ||
a = A(100) | ||
# print(a.x) | ||
# print(a.y) | ||
c = getattr(a, 'y', 200) | ||
# print("========: ", c) | ||
print(a.__dict__['x']) | ||
print(dir(a)) | ||
|
||
|
||
# __get()__ demo | ||
# 描述符,可以将访问对象属性转变为调用描述符方法。 | ||
# 描述符可以让我们在获取或者给对象赋值时对数据值进行一些特殊的加工和处理; | ||
|
||
|
||
|
||
# property | ||
class Item: | ||
@property # 等价于price = property(price),也就是实例化了一个描述符对象price | ||
def price(self): | ||
print("~~~~~~") | ||
return self._price | ||
|
||
# 使用描述符对象的setter方法对price进行装饰(price = price.setter(price)), | ||
# 这里就是将设置属性值的方法传入描述符内,修改price属性时就会通过__set__方法调用传入的方法 | ||
@price.setter | ||
def price(self, value): | ||
print("*************") | ||
if value > 0: # 校验price | ||
self._price = value | ||
else: | ||
raise ValueError("Valid value..") | ||
|
||
def property_demo(): | ||
item = Item() | ||
item.price = 100 | ||
print(item.price) | ||
# item.price = -100 | ||
print(item.__dict__) | ||
|
||
if __name__ == "__main__": | ||
# getattribute_demo() | ||
# getattr_demo() | ||
property_demo() | ||
print("run attr_method successfully !!!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
class MyProperty(object): | ||
def __init__(self, fget=None, fset=None, fdel=None, doc=None): | ||
self.fget = fget | ||
self.fset = fset | ||
self.fdel = fdel | ||
self.__doc__ = doc | ||
|
||
def __get__(self, obj, objtype=None): | ||
print("====>in __get__") | ||
if obj is None: | ||
return self | ||
if self.fget is None: | ||
raise AttributeError | ||
return self.fget(obj) | ||
|
||
def __set__(self, obj, value): | ||
print("====>in __set__") | ||
if self.fset is None: | ||
raise AttributeError | ||
self.fset(obj, value) | ||
|
||
def __delete__(self, obj): | ||
print("====>in __delete__") | ||
if self.fdel is None: | ||
raise AttributeError | ||
self.fdel(obj) | ||
|
||
def getter(self, fget): | ||
pass | ||
|
||
def setter(self, fset): | ||
print("====>in setter") | ||
return type(self)(self.fget, fset, self.fdel, self.__doc__) | ||
|
||
def deleter(self, fdel): | ||
print("====>in deleter") | ||
return type(self)(self.fget, self.fset, fdel, self.__doc__) | ||
|
||
class Item: | ||
@MyProperty | ||
def price(self): | ||
return self._price | ||
|
||
@price.setter | ||
def price(self, value): | ||
if value > 0: | ||
self._price = value | ||
else: | ||
raise ValueError("Valid value..") | ||
|
||
def func1(): | ||
item = Item() | ||
item.price = 100 | ||
print(item.price) | ||
|
||
if __name__ == "__main__": | ||
func1() | ||
print("run property_demo.py successfully !!!") |