Skip to main content

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

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-2.0.0.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simple_factory_pattern-2.0.0-py3-none-any.whl (15.7 kB view details)

Uploaded Python 3

File details

Details for the file simple_factory_pattern-2.0.0.tar.gz.

File metadata

  • Download URL: simple_factory_pattern-2.0.0.tar.gz
  • Upload date:
  • Size: 14.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.7.17 Linux/6.2.0-1012-azure

File hashes

Hashes for simple_factory_pattern-2.0.0.tar.gz
Algorithm Hash digest
SHA256 1406f9b9e2b2b5360570840469545512a552a709ec6632780da86c5d904b36d9
MD5 3f51218ee8107ae55e015c41edd243de
BLAKE2b-256 e075dd31559f735150c0332f27a403a52e13cdb87787e31a7399fa9e6f8bfa80

See more details on using hashes here.

File details

Details for the file simple_factory_pattern-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for simple_factory_pattern-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 aee1ed36d8985c4cf3f2093e00b251a2ed8a400753875af678ae12bde0faad77
MD5 e77e92836a68004ac2b0f70b45c5e66f
BLAKE2b-256 6a3c6484481a7c4716a5b6cceea867fda679ea42b1d3fcdd7b7915a26d20e74a

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 files

1.3.1

2 files

1.3

2 files

1.2

2 files

1.1.1

2 files

1.1

2 files

1.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page