Simple way to prevent direct creation of an instance of a class.
Project description
Simple Python Factory pattern
This module provides a simple way to prevent the direct creation of an instance of a class. Instead a classmethod
of this class can be used.
Install
You can install this python module via pip:
pip install simple-factory-pattern
Otherwise the module can be downloaded from PyPI: https://pypi.org/project/simple-factory-pattern/
Usage
-
Import the module:
from simple_factory_pattern import FactoryPatternMeta
-
Create a class that uses the above meta class:
class NewClass(metaclass=FactoryPatternMeta): pass
-
Add a
classmethod
to the new class that returns an instance of the class:@classmethod def create_instance(cls): return NewClass()
You can choose any name for the method. But it must be a
classmethod
!It's also possible to define multiple
classmethod
s. Only in those methods a new instance of the class can be created.
Behavior of the new class
It's not possible to create an instance of the class directly:
instance = NewClass() # this will fail
This throws a FactoryException
(also included in the simple_factory_pattern
package).
The only way to get an instance is to call the above definded classmethod
:
instance = NewClass.create_instance() # this works
Project details
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
File details
Details for the file simple-factory-pattern-1.2.tar.gz
.
File metadata
- Download URL: simple-factory-pattern-1.2.tar.gz
- Upload date:
- Size: 16.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.6.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9161ccb66c514e510442d782be85503341a7621b2c4caf443faba6ccf36285f3 |
|
MD5 | 80bf4cbeccf91ae52e927810356a7b69 |
|
BLAKE2b-256 | d64a740fea82d73f57806570e06306649224b96f705e703b5b5d6b964faef6d3 |
File details
Details for the file simple_factory_pattern-1.2-py3-none-any.whl
.
File metadata
- Download URL: simple_factory_pattern-1.2-py3-none-any.whl
- Upload date:
- Size: 15.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.6.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7e27a65497c354759be63687b873961a2299f3dfe90ef71ae1cf9efb98349758 |
|
MD5 | 1efbbcdebfaaede287c2435698475d35 |
|
BLAKE2b-256 | 38bffc2edcfbe0f24d65c41b84157169a10ec8c42e5cc1e80feacfd8f8dc0778 |