Hybrid-Attributes Documentation¶
Hybrid-Attributes provides a hybrid_property
and a
hybrid_method
descriptor, which call the underlying
function both in a class and instance context:
class SomeClass:
@hybrid_property
def spam(self):
return 'spam'
@hybrid_method
def eggs(self):
return 'eggs'
>>> SomeClass.spam
'spam'
>>> SomeClass.eggs()
'eggs'
>>> SomeClass().spam
'spam'
>>> SomeClass().eggs()
'eggs'
Installation¶
Hybrid-Attributes is available on the PyPI and can be installed with pip:
pip install hybrid-attributes
API Reference¶
-
class
hybrid_attributes.
hybrid_property
(fget=None, fset=None, fdel=None, fcget=None)¶ A property like descriptor that gives you the ability to produce values when accessed on a class.
Parameters: - fget – A function that is called with the instance or class the property is accessed on, the result will be returned as attribute.
- fset – A function that will be called when a value is assigned to the attribute on an instance.
- fdel – A function that is called when the attribute is deleted on an instance.
- fcget –
A function that is called when the attribute is accessed on a class, with that class as argument.
Use this when you need to do something different, depending on whether the attribute is accessed on a class or an instance.
If any of these functions are missing, an
AttributeError
will be raised when a user performs an operation that would otherwise lead to such a function being called.Instead of passing all arguments directly, you should use decorators to define the different functions:
class SomeClass: @hybrid_property def attribute(self): return self._attribute @attribute.classgetter def attribute(cls): return 'SomeClass.attribute was accessed' @attribute.setter def attribute(self, value): self._attribute = value @attribute.deleter def attribute(self, value): del self._attribute
-
classgetter
(fcget)¶ Returns a new hybrid_property instance with fcget set to the given function.
-
deleter
(fdel)¶ Returns a new hybrid_property instance with fdel set to the given function.
-
getter
(fget)¶ Returns a new hybrid_property instance with fget set to the given function.
-
setter
(fset)¶ Returns a new hybrid_property instance with fset set to the given function.
-
class
hybrid_attributes.
hybrid_method
(function, class_function=None)¶ A descriptor that binds functions to whatever object, class or instance, it has been accessed on. This allows you to use a function as an instance method and a class method at the same time.
Parameters: - function – A function that is bound to the class or instance and returned, when the attribute is accessed on a class or instance respectively.
- class_function –
A function that is bound to the class, instead of function, when the attribute is accessed on a class.
Use this when you need different behavior when the method is called on a class.
Instead of passing all arguments directly, you should use the classmethod decorator to define the class_function:
class SomeClass: @hybrid_method def spam(self): return 'called on a SomeClass instance' @spam.classmethod def spam(cls): return 'called on SomeClass'
-
classmethod
(class_function)¶ Returns a new hybrid_method instance with class_function set to the given function.