PEP487 - Simpler customisation of class creation
Project description
This is a backport of PEP487’s simpler customisation of class creation by Martin Teichmann <https://www.python.org/dev/peps/pep-0487/> for Python versions before 3.6.
PEP487 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Subclass init
>>> from pep487 import PEP487Object >>> class FooBase(PEP487Object): ... foos = set() ... ... def __init_subclass__(cls, **kwargs): ... cls.foos.add(cls.__name__)
Using PEP487Object as base class all subclasses of FooBase will add their name to the common class variable ‘foos’.
>>> class Foo1(FooBase): ... pass >>> class Foo2(FooBase): ... pass
Hence:
>>> FooBase.foos
{'Foo1', 'Foo2'}
Property names and owner
If a class object has a method __set_name__ upon declaration of an PEP487Object class, it will be called:
>>> class NamedProperty: ... def __set_name__(self, owner, name): ... self.context = owner ... self.name = name
>>> class Bar(PEP487Object): ... foo = NamedProperty() ... bar = NamedProperty()
Consequently:
>>> Bar.foo.name is 'foo' and Bar.foo.context is Bar True >>> Bar.bar.name is 'bar' and Bar.bar.context is Bar True
Abstract base classes
Since PEP487Object has a custom metaclass, it is incompatible to abc.ABC. Therefore pep487 contains patched versions of ABC and ABCMeta.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pep487-1.0.1.tar.gz.
File metadata
- Download URL: pep487-1.0.1.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ccca1d5b70fce64a1d52bdc3a873b47d7d67fe5b2fbf8c303f102c83f3458bfd
|
|
| MD5 |
0f70ef21c22a88edaa74dce9fa6da394
|
|
| BLAKE2b-256 |
44582ef69797a40ea69c0d204d5d03bb8a1cb1ea278c510b52deb01e48d07231
|
File details
Details for the file pep487-1.0.1-py2.py3-none-any.whl.
File metadata
- Download URL: pep487-1.0.1-py2.py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8d6545f261df40becc95cceeb2cd75fc736e04731da48eb54e01e7e227060b39
|
|
| MD5 |
fd472c5b3db4dbe8f805b4fc856fe99a
|
|
| BLAKE2b-256 |
594e70a2b16a5cb39f690c6708c9db4ff003ce0c05aed12e95a7e3705df8b4eb
|