Update examples
This commit is contained in:
42
set_get_attr_example.py
Normal file → Executable file
42
set_get_attr_example.py
Normal file → Executable file
@ -1,3 +1,37 @@
|
||||
"""
|
||||
set_get_attr_example.py
|
||||
-----------------------
|
||||
|
||||
This module contains a class named `Number` which implements special attribute
|
||||
access methods to demonstrate custom attribute setting and getting behaviors
|
||||
in a Python class.
|
||||
|
||||
Here is a summary of its functionality and methods:
|
||||
|
||||
- `__init__(self, number)`: Initializes a new instance with a `number` parameter.
|
||||
- `power(self)`: Squares the current value of the `_number` attribute. It utilizes
|
||||
`self.number` to access the `_number` attribute, which works correctly due to
|
||||
the overridden `__getattribute__` method.
|
||||
- `another_func(self)`: Prints a message when invoked.
|
||||
- `__str__(self)`: Returns a string representation of the `_number` attribute.
|
||||
|
||||
The class also overrides special methods (`__setattr__`, `__getattribute__`,
|
||||
`__getattr__`) to define custom behaviors for setting, getting, and accessing
|
||||
non-existent attributes, respectively.
|
||||
|
||||
Usage:
|
||||
If run as the main program, an instance of `Number` is created with an initial
|
||||
value of 5. The script then performs various operations to demonstrate the
|
||||
custom attribute access behaviors.
|
||||
|
||||
Note:
|
||||
The `__getattribute__` method in the `Number` class is implemented such that if
|
||||
an attribute other than the ones listed in the `options` list is accessed, the
|
||||
`_number` attribute value is returned. This is demonstrated in the script where
|
||||
`number.num` and `number.number` (which are non-existing attributes) are accessed,
|
||||
but the `_number` value is printed instead of raising an AttributeError.
|
||||
"""
|
||||
|
||||
class Number:
|
||||
def __init__(self, number):
|
||||
self._number = number
|
||||
@ -31,3 +65,11 @@ class Number:
|
||||
|
||||
def __getattr__(self, name):
|
||||
return self._number
|
||||
|
||||
if __name__ == "__main__":
|
||||
number = Number(5)
|
||||
print(number.num)
|
||||
print(number.number)
|
||||
number.power()
|
||||
print(number.num)
|
||||
number.another_func()
|
||||
|
Reference in New Issue
Block a user