super().__setattr__('training', True)
时间: 2024-06-15 12:04:10 浏览: 26
super().__setattr__('training', True)是Python中的一种特殊方法调用方式,用于在子类中调用父类的方法。在这个例子中,它调用了父类的__setattr__方法,并将'training'属性设置为True。
__setattr__是Python中的一个特殊方法,用于设置对象的属性。当我们使用赋值语句给对象的属性赋值时,实际上是调用了该对象的__setattr__方法。在这个例子中,通过调用super().__setattr__('training', True),我们可以在子类中调用父类的__setattr__方法来设置属性。
需要注意的是,使用super().__setattr__('training', True)时,我们必须确保父类中存在名为__setattr__的方法,并且该方法接受两个参数:self和name。否则,会引发AttributeError异常。
相关问题
super().__setattr__
`super().__setattr__()` is a built-in Python function that can be used to call the `__setattr__()` method of the parent class in a subclass. It is commonly used when you want to add some additional functionality to the `__setattr__()` method of the parent class without completely overriding it.
Here's an example:
```python
class MyClass:
def __setattr__(self, name, value):
print(f"Setting {name} to {value}")
super().__setattr__(name, value)
class MySubclass(MyClass):
def __setattr__(self, name, value):
print(f"Adding extra functionality for {name} = {value}")
super().__setattr__(name, value.upper())
```
In this example, `MySubclass` is a subclass of `MyClass`. It overrides the `__setattr__()` method of its parent class to add some extra functionality. However, it still wants to call the `__setattr__()` method of the parent class to handle the actual setting of the attribute.
To do this, it uses `super().__setattr__(name, value.upper())` to call the `__setattr__()` method of the parent class and pass in the modified value. This allows it to add its own functionality while still preserving the functionality of the parent class.
super().__setattr__是什么
`super().__setattr__` 是 Python 中的内置函数,用于在子类中对父类属性进行赋值操作。它可以调用父类的方法来设置属性,而不影响子类自身的属性。具体来说,它可以用于在子类中重载父类属性,或者在子类中增加新的属性。在调用 `super().__setattr__` 的时候,需要传入两个参数:第一个参数是子类的类名,第二个参数是属性名和属性值的键值对。例如:
```python
class Parent:
def __init__(self):
self._x = None
def get_x(self):
return self._x
def set_x(self, value):
self._x = value
class Child(Parent):
def __init__(self):
super().__init__()
self._y = None
def get_y(self):
return self._y
def set_y(self, value):
self._y = value
def set_x(self, value):
super().__setattr__('_x', value * 2)
```
在上面的代码中,`Child` 继承自 `Parent`,并在 `set_x` 方法中调用了 `super().__setattr__` 来设置 `_x` 属性的值。这样,当我们在子类中调用 `set_x` 方法时,实际上调用的是父类的 `set_x` 方法,并且将输入值乘以 2 后再进行赋值操作。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)