Welcome to jaraco.classes documentation!

For Enterprise

Professional support for jaraco.classes is available as part of the Tidelift Subscription. Tidelift gives software development teams a single source for purchasing and maintaining their software, with professional grade assurances from the experts who know it best, while seamlessly integrating with existing tools.

Learn more Request a Demo

Routines for obtaining the class names of an object and its parent classes.

jaraco.classes.ancestry.all_bases(c)

return a tuple of all base classes the class c has as a parent. >>> object in all_bases(list) True

jaraco.classes.ancestry.all_classes(c)

return a tuple of all classes to which c belongs >>> list in all_classes(list) True

jaraco.classes.ancestry.iter_subclasses(cls)

Generator over all subclasses of a given class, in depth-first order.

>>> bool in list(iter_subclasses(int))
True
>>> class A(object): pass
>>> class B(A): pass
>>> class C(A): pass
>>> class D(B,C): pass
>>> class E(D): pass
>>>
>>> for cls in iter_subclasses(A):
...     print(cls.__name__)
B
D
E
C
>>> # get ALL classes currently defined
>>> res = [cls.__name__ for cls in iter_subclasses(object)]
>>> 'type' in res
True
>>> 'tuple' in res
True
>>> len(res) > 100
True

meta.py

Some useful metaclasses.

class jaraco.classes.meta.LeafClassesMeta(name, bases, attrs)

Bases: type

A metaclass for classes that keeps track of all of them that aren’t base classes.

>>> Parent = LeafClassesMeta('MyParentClass', (), {})
>>> Parent in Parent._leaf_classes
True
>>> Child = LeafClassesMeta('MyChildClass', (Parent,), {})
>>> Child in Parent._leaf_classes
True
>>> Parent in Parent._leaf_classes
False
>>> Other = LeafClassesMeta('OtherClass', (), {})
>>> Parent in Other._leaf_classes
False
>>> len(Other._leaf_classes)
1
class jaraco.classes.meta.TagRegistered(name, bases, namespace)

Bases: type

As classes of this metaclass are created, they keep a registry in the base class of all classes by a class attribute, indicated by attr_name.

>>> FooObject = TagRegistered('FooObject', (), dict(tag='foo'))
>>> FooObject._registry['foo'] is FooObject
True
>>> BarObject = TagRegistered('Barobject', (FooObject,), dict(tag='bar'))
>>> FooObject._registry is BarObject._registry
True
>>> len(FooObject._registry)
2

‘…’ below should be ‘jaraco.classes’ but for pytest-dev/pytest#3396 >>> FooObject._registry[‘bar’] <class ‘….meta.Barobject’>

attr_name = 'tag'
class jaraco.classes.properties.NonDataProperty(fget)

Bases: object

Much like the property builtin, but only implements __get__, making it a non-data property, and can be subsequently reset.

See http://users.rcn.com/python/download/Descriptor.htm for more information.

>>> class X(object):
...   @NonDataProperty
...   def foo(self):
...     return 3
>>> x = X()
>>> x.foo
3
>>> x.foo = 4
>>> x.foo
4

‘…’ below should be ‘jaraco.classes’ but for pytest-dev/pytest#3396 >>> X.foo <….properties.NonDataProperty object at …>

class jaraco.classes.properties.classproperty(fget, fset=None)

Bases: object

Like @property but applies at the class level.

>>> class X(metaclass=classproperty.Meta):
...   val = None
...   @classproperty
...   def foo(cls):
...     return cls.val
...   @foo.setter
...   def foo(cls, val):
...     cls.val = val
>>> X.foo
>>> X.foo = 3
>>> X.foo
3
>>> x = X()
>>> x.foo
3
>>> X.foo = 4
>>> x.foo
4

Setting the property on an instance affects the class.

>>> x.foo = 5
>>> x.foo
5
>>> X.foo
5
>>> vars(x)
{}
>>> X().foo
5

Attempting to set an attribute where no setter was defined results in an AttributeError:

>>> class GetOnly(metaclass=classproperty.Meta):
...   @classproperty
...   def foo(cls):
...     return 'bar'
>>> GetOnly.foo = 3
Traceback (most recent call last):
...
AttributeError: can't set attribute

It is also possible to wrap a classmethod or staticmethod in a classproperty.

>>> class Static(metaclass=classproperty.Meta):
...   @classproperty
...   @classmethod
...   def foo(cls):
...     return 'foo'
...   @classproperty
...   @staticmethod
...   def bar():
...     return 'bar'
>>> Static.foo
'foo'
>>> Static.bar
'bar'

Legacy

For compatibility, if the metaclass isn’t specified, the legacy behavior will be invoked.

>>> class X:
...   val = None
...   @classproperty
...   def foo(cls):
...     return cls.val
...   @foo.setter
...   def foo(cls, val):
...     cls.val = val
>>> X.foo
>>> X.foo = 3
>>> X.foo
3
>>> x = X()
>>> x.foo
3
>>> X.foo = 4
>>> x.foo
4

Note, because the metaclass was not specified, setting a value on an instance does not have the intended effect.

>>> x.foo = 5
>>> x.foo
5
>>> X.foo  # should be 5
4
>>> vars(x)  # should be empty
{'foo': 5}
>>> X().foo  # should be 5
4
class Meta

Bases: type

setter(fset)

Indices and tables