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

Uploaded Source

Built Distribution

simple_factory_pattern-1.0-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

Details for the file simple-factory-pattern-1.0.tar.gz.

File metadata

  • Download URL: simple-factory-pattern-1.0.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.5.9

File hashes

Hashes for simple-factory-pattern-1.0.tar.gz
Algorithm Hash digest
SHA256 af8c1d3d3208b9b4b77e4326627c1e39dee1ef06adea0e84f4be9a48c23fb2b5
MD5 01db99cb8b2dec79764310f75e035a64
BLAKE2b-256 576bbd616faa04294e5db9bff8c35afa721fe4d5d368a2f1ef9d3e7c0138400b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: simple_factory_pattern-1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.5.9

File hashes

Hashes for simple_factory_pattern-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1a93c8e1ff21f0e8e0c85a373deb7519b18ef90bb704d7b5321be999d5ef9210
MD5 135893d8c61b39290f8f59903c008272
BLAKE2b-256 71aa5849ca148fb7741d51469a8f070f460feddf06292763aa82af615899bd15

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