meta data for this page
  •  

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
programming:python:oop [2024/01/02 10:08]
niziak
programming:python:oop [2024/01/03 11:34] (current)
niziak
Line 2: Line 2:
  
 ===== special methods ===== ===== special methods =====
 +[[https://​docs.python.org/​3/​reference/​datamodel.html#​special-method-names]]
  
   * [[https://​docs.python.org/​3/​reference/​datamodel.html#​object.__new__|__new__]]   * [[https://​docs.python.org/​3/​reference/​datamodel.html#​object.__new__|__new__]]
Line 7: Line 8:
   * [[https://​docs.python.org/​3/​reference/​datamodel.html#​object.__del__|__del__]] - finalizer: called when the instance is about to be destroyed.   * [[https://​docs.python.org/​3/​reference/​datamodel.html#​object.__del__|__del__]] - finalizer: called when the instance is about to be destroyed.
     * It is not guaranteed that __del__() methods are called for objects that still exist when the interpreter exits.     * It is not guaranteed that __del__() methods are called for objects that still exist when the interpreter exits.
 +    * **Note**: ''​del x''​ doesn’t directly call ''​x.__del__()''​ — the former decrements the reference count for x by one, and the latter is only called when x’s reference count reaches zero. 
 +  * [[https://​docs.python.org/​3/​reference/​datamodel.html#​object.__repr__|__repr__]] - Called by the ''​repr()''​ built-in function to compute the “official” string representation of an object. This is typically used for debugging, so it is important that the representation is information-rich and unambiguous
 +  * [[https://​docs.python.org/​3/​reference/​datamodel.html#​object.__str__|__str__]] - Called by ''​str(object)''​ and the built-in functions ''​format()''​ and ''​print()''​ to compute the “informal” or nicely printable string representation of an object.
 +  * ''​__bytes__''​
 +  * ''​__format__''​
 +  * ''​__lt__ / __le__ / __eq__ / __ne__ / __gt__ / __ge__''​
 +  * ''​__hash__''​
 +  * ''​__bool__''​
  
 ===== call super constructor ===== ===== call super constructor =====
Line 49: Line 58:
   * **property** - properties are methods accessed like attributes. It gives full control on its getter, setter and deleter access.   * **property** - properties are methods accessed like attributes. It gives full control on its getter, setter and deleter access.
  
 +
 +**delattr(object,​ name)**
 +This is a relative of ''​setattr()''​. The arguments are an object and a string. The string must be the name of one of the object’s attributes. The function deletes the named attribute, provided the object allows it. For example, ''​delattr(x,​ '​foobar'​)''​ is equivalent to ''​del x.foobar''​. name need not be a Python identifier (see setattr()).
 +
 +
 +===== Object-like attribute access for nested dictionary =====
 +  * https://​stackoverflow.com/​questions/​38034377/​object-like-attribute-access-for-nested-dictionary
 +  * https://​bobbyhadz.com/​blog/​python-use-dot-to-access-dictionary
 +  * https://​github.com/​frmdstryr/​magicattr