__dict__, __setattr__ , __getattr__ , __getattribute__
__dict__
中class myClass():
def __init__(self):
print(self.__dict__)
self.name = "Alice"
print(self.__dict__)
print(type(self.__dict__))
c = myClass()
"""
{}
{'name': 'Alice'}
<class 'dict'>
"""
__setattr__
和 __getattribute__
/ __getattr__
方法__getattribute__
,否则才会再进入 __getattr__
__getattribute__
中返回 self.xxx
当然会递归报错,需要调用 object.__getattribute()
__getattribute__
中写 return self__dict__[xxx]
from typing import Any
class myClass():
def __init__(self):
self.name = "Alice"
def __setattr__(self, __name: str, __value: Any) -> None:
print("Now set " + __name + " to " + __value)
self.__dict__[__name] = __value
def __getattribute__(self, __name: str) -> Any:
print("__getattribute__ : Now get " + __name)
return object.__getattribute__(self, __name)
def __getattr__(self, __name: str) -> Any:
print("__getattr__ : Now get " + __name)
if __name in self.__dict__:
return self.__dict__[__name]
else:
return "Not Exist"
c = myClass()
print("\n**********\n")
print(c.name)
print("\n**********\n")
print(c.name2)
"""
Now set name to Alice
__getattribute__ : Now get __dict__
**********
__getattribute__ : Now get name
Alice
**********
__getattribute__ : Now get name2
__getattr__ : Now get name2
__getattribute__ : Now get __dict__
Not Exist
"""
__delattr__(self, __name)
,在删除该属性的时候调用,不赘述了。hasattr(obj, name)
super()
super().xxx
调用直接继承父类的方法。CB
类的 super().__init__()
注释掉之后CC
类调用 super().__init__()
是没法进入到 CA
类中去的,尽管 CC
类是间接继承自 CA
类。from typing import Any
class CA():
def __init__(self):
print("In A")
self.name = "Alice"
class CB(CA):
def __init__(self):
print("In B")
super().__init__()
self.name = "Bob"
class CC(CB):
def __init__(self):
print("In C")
super().__init__()
self.name = "Cindy"
A = CA()
print("\n*****************\n")
B = CB()
print("\n*****************\n")
C = CC()
print("\n*****************\n")
print(A.name)
print(B.name)
print(C.name)
"""
In A
*****************
In B
In A
*****************
In C
In B
In A
*****************
Alice
Bob
Cindy
"""
super().__setattr__(__name, __value)
super().__getattr__(__name)
注意:也可以直接 super().name
之类的,获得父类的属性。
但使用 super().__setattr__()
貌似更规范
from typing import Any
def foo(x : str):
print("TYPE : " + str(type(x)))
print("VALUE : " + str(x))
foo({"123":456})
foo("123")
foo(123)
foo(["123"])
foo
"""
TYPE : <class 'dict'>
VALUE : {'123': 456}
TYPE : <class 'str'>
VALUE : 123
TYPE : <class 'int'>
VALUE : 123
TYPE : <class 'list'>
VALUE : ['123']
"""
typing
包List是来源于typing.List, 而小写的list来源是class 'list'
def foo(x : list):
pass
from typing import List
def foo2(x : List):
pass
module: Optional['Module']
,那么就写:if not isinstance(module, Module) and module is not None:
raise TypeError(f"{torch.typename(module)} is not a Module subclass")
或者多用 assert
诊断也可以
unpacking
a = (1,2)
b, c = a
print(b,c)
a = (1,2)
(b, c) = a
print(b,c)
a = [1,2]
(b, c) = a
print(b,c)
a = [1,2,3,4,5]
b, *c = a
print(b,c)
"""
1 2
1 2
1 2
1 [2, 3, 4, 5]
"""
a,b,c = "123"
print(a,b,c)
a,b = 1,2
a,b = b,a
print(a,b)
a = 1,
print(a)
a, = [1]
print(a)
a, *b, c = [1,2,3,4,5]
print(a,b,c)
"""
1 2 3
2 1
(1,)
1
1 [2, 3, 4] 5
"""
a = [1,2,3,4]
b = ['a','b','c',6 ,7]
for x,y in zip(a,b):
print(x,y)
print(list(zip(a,b)))
"""
1 a
2 b
3 c
4 6
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 6)]
"""
a = [1,2,3,4]
b = ['a','b','c',6 ,7]
dc = dict(zip(a,b))
for x,y in dc.items():
print(x,y)
"""
1 a
2 b
3 c
4 6
"""