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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: simple-factory-pattern-1.3.1.tar.gz
  • Upload date:
  • Size: 15.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.0 CPython/3.7.13 Linux/5.15.0-1017-azure

File hashes

Hashes for simple-factory-pattern-1.3.1.tar.gz
Algorithm Hash digest
SHA256 3d259240d9e933a14026a940ce52c7939ec8c0bff12df10391644855701edabb
MD5 2bea42f6bde285d3e47a8d92871ff196
BLAKE2b-256 77dec4e7c412d4100ff7f2adc30b2782de24337286ccae41997efe69c74721ba

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for simple_factory_pattern-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ab8c5b471a7fc7e72506b44ea4b1cf5e2150bcd6e3616a32eb31781ff64ad636
MD5 5f33fa7820ef1362d64e02e1f1a1cf23
BLAKE2b-256 1ce66324978c4e5cd7cac974717f77f78812ebd015fd952f1230f18fb564bce1

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