Skip to main content

Simple way to prevent direct creation of an instance of a class.

Project description

Simple Python Factory pattern

PyPI package PyPI version

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

  1. Import the module:

    from simple_factory_pattern import FactoryPatternMeta
    
  2. Create a class that uses the above meta class:

    class NewClass(metaclass=FactoryPatternMeta):
        pass
    
  3. 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 classmethods. 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

simple-factory-pattern-1.2.tar.gz (16.1 kB view details)

Uploaded Source

Built Distribution

simple_factory_pattern-1.2-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

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

Hashes for simple-factory-pattern-1.2.tar.gz
Algorithm Hash digest
SHA256 9161ccb66c514e510442d782be85503341a7621b2c4caf443faba6ccf36285f3
MD5 80bf4cbeccf91ae52e927810356a7b69
BLAKE2b-256 d64a740fea82d73f57806570e06306649224b96f705e703b5b5d6b964faef6d3

See more details on using hashes here.

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

Hashes for simple_factory_pattern-1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7e27a65497c354759be63687b873961a2299f3dfe90ef71ae1cf9efb98349758
MD5 1efbbcdebfaaede287c2435698475d35
BLAKE2b-256 38bffc2edcfbe0f24d65c41b84157169a10ec8c42e5cc1e80feacfd8f8dc0778

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page