Home > Article > Backend Development > Seven Python questions to improve literacy
I have gained something from these 7 questions, which are summarized as follows:
You may know the magic functions in Python, such as __add__ and __sub__ Represents the - operator, which represents obj /- something, but you may not know that there is also a __radd__, __rsub__ function, which can represent something /- obj.
For example:
class Dog: def __add__(self, other): return "from __add__" def __radd__(self, other): return "from __radd__" dog = Dog() print(dog + 1) # from __add__ print(1 + dog) # from __radd__
__getattr__ The magic method will only be called when we try to get an attribute that does not exist, __getattribute__ will be called every time This is called every time we try to access a property.
The code is as follows:
class Dog: def __init__(self, name, age): self.name = name self.age = age def __getattr__(self, key): return f"{key} not found" dog = Dog("taidi", 5) print(dog.name)# taidi print(dog.age) # 5 print(dog.breed) # breed not found
class Dog: def __init__(self, name, age): self.name = name self.age = age def __getattribute__(self, key): return f"{key} not found" dog = Dog("taidi", 5) print(dog.name)# name not found print(dog.age) # age not found print(dog.breed) # breed not found
class Animal: def __init__(self, name, age): self.name = name self.age = age class Dog(Animal): def __init__(self, name, age, breed): super().__init__(name, age) self.breed = breed
is equivalent to:
class Animal: def __init__(self, name, age): self.name = name self.age = age class Dog(Animal): def __init__(self, name, age, breed): Animal.__init__(self, name, age) self.breed = breed
Please note , Animal.__init__(self, name, age) cannot be missing the self parameter.
class Animal: pass class Dog(Animal): pass class Cat(Animal): pass class GermanSheperd(Dog): pass print(Animal.__subclasses__()) # [<class '__main__.Dog'>, <class '__main__.Cat'>]
However, .__subclasses__() can only check direct subclasses.
class A: def test(self): print("A") class B: def test(self): print("B") class C(A, B): pass C().test() # A
A and B both have test methods, so which one does C integrate? In Python, the leftmost class takes precedence.
Here, A is the leftmost parent class, so A's test method is integrated.
Multiple recharges are confusing, so it’s better not to use them.
class Dog: def __invert__(self): return "test" dog = Dog() print(~dog) # test
~ The operator stands for "bitwise not" and is usually used to invert content. A more meaningful example is as follows:
class Coordinate: def __init__(self, x, y): self.x = x self.y = y def __str__(self): return f"({self.x}, {self.y})" def __invert__(self): return Coordinate(-self.x, -self.y) a = Coordinate(3, 4) b = ~a print(a, b) # (3, 4) (-3, -4)
def init(self, name, age): self.name = name self.age = age def bark(self): print("woof") Dog = type("Dog", (), {"__init__":init, "bark":bark}) dog = Dog("taidi", 10) print(dog.name) print(dog.age) # taidi # 10
Here, we pass 3 parameters to type to create our class.
The first parameter __name__ is the name of the class. The second parameter __bases__ is a tuple containing the parent class. The third parameter __dict__ is a dictionary containing attributes and methods.
Equivalent to:
class Dog: def __init__(self, name, age): self.name = name self.age = age def bark(self): print("woof")
The above is the detailed content of Seven Python questions to improve literacy. For more information, please follow other related articles on the PHP Chinese website!