cardinal_pythonlib.classes
Original code copyright (C) 2009-2022 Rudolf Cardinal (rudolf@pobox.com).
This file is part of cardinal_pythonlib.
Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Functions to help work with Python classes.
- class cardinal_pythonlib.classes.ClassProperty(fget=None, fset=None, fdel=None, doc=None)[source]
One way to mark a function as a class property (logically, a combination of
@classmethod
and@property
).See https://stackoverflow.com/questions/128573/using-property-on-classmethods.
However, in practice we use
classproperty
, a slightly different version.
- cardinal_pythonlib.classes.all_subclasses(cls: Type) List[Type] [source]
Returns all subclasses of a class.
- Parameters:
cls¶ – a class
- Returns:
a list of all subclasses
- cardinal_pythonlib.classes.class_attribute_dict(cls: Type, exclude_underscore: bool = True, exclude_double_underscore: bool = True) Dict[str, Any] [source]
When given a class, returns a dictionary of all its attributes, by default excluding those starting with single and double underscores.
This is just a filtered versio of cls.__dict__.
- cardinal_pythonlib.classes.class_attribute_names(cls: Type, exclude_underscore: bool = True, exclude_double_underscore: bool = True) List[str] [source]
When given a class, returns the NAMES of all its attributes, by default excluding those starting with single and double underscores.
Used in particular to enumerate constants provided within a class.
- cardinal_pythonlib.classes.class_attribute_values(cls: Type, exclude_underscore: bool = True, exclude_double_underscore: bool = True) List[str] [source]
When given a class, returns the VALUES of all its attributes, by default excluding those starting with single and double underscores.
Used in particular to enumerate constants provided within a class.
- class cardinal_pythonlib.classes.classproperty(fget)[source]
Decorator to mark a function as a class property (logically, a combination of
@classmethod
and@property
).See https://stackoverflow.com/questions/128573/using-property-on-classmethods
- cardinal_pythonlib.classes.derived_class_implements_method(derived: Type[T1], base: Type[T2], method_name: str) bool [source]
Does a derived class implement a method (and not just inherit a base class’s version)?
- Parameters:
- Returns:
whether the derived class method is (a) present, and (b) different to the base class’s version of the same method
Note: if C derives from B derives from A, then a check on C versus A will succeed if C implements the method, or if C inherits it from B but B has re-implemented it compared to A.