If you want a singleton in python, think of the module as a singleton

I think.

If you want a reference to yourself, in the module

import sys
self = sys.modules[__name__]

It is convenient to write such as. If you are using python3 series

import importlib
self = importlib.import_module(__name__)

But it's okay.

The reason why I suddenly wanted to say this is because I was looking at the code I wrote a long time ago and thought that it was really black history. All you have to do is define the create function in a module called hogefactory.py.

somemodule.py


class HogeFactory:
    @staticmethod
    def create(params):
        """A method that creates and returns a Hoge object"""
        #Various complicated processing
        # ...
        return Hoge(some_args, some_kwargs)

As an aside, when should I use the static method of python? I don't remember writing recently.

Recommended Posts

If you want a singleton in python, think of the module as a singleton
If you want to assign csv export to a variable in python
Get the caller of a function in Python
Make a copy of the list in Python
[Python] If you want to draw a scatter plot of multiple clusters
[Python] A rough understanding of the logging module
Output in the form of a python array
What happens if you do "import A, B as C" in Python?
If you want to switch the execution user in the middle of a Fabric task, settings context manager
A solution if you accidentally remove the python interpreter in / usr / local / bin /.
I want to see a list of WebDAV files in the Requests module
[python] Get the list of classes defined in the module
If you encounter a "Unicode Decode Error" in Python
A reminder about the implementation of recommendations in Python
If you define a method in a Ruby class and define a method in it, it becomes a method of the original class.
How to check in Python if one of the elements of a list is in another list
Find out the apparent width of a string in python
Why do you add a main ()-if statement in Python?
Get the number of specific elements in a python list
[Note] Import of a file in the parent directory in Python
Find the eigenvalues of a real symmetric matrix in Python
Make a note of what you want to do in the future with Raspberry Pi
Implement the Singleton pattern in Python
Use blender as a python module
Solution if the module is installed in Python but you get an error in Jupyter notebook
If you want Python completion in VS Code, Microsoft Python Language Server is the best story
Master the weakref module in Python
[Python] If you create a file with the same name as the module to be imported, an Attribute Error will occur.
How to determine the existence of a selenium element in Python
Save the result of the life game as a gif with python
What to do if you get a minus zero in Python
How to check the memory size of a variable in Python
Delete a particular character in Python if it is the last
If you give a list with the default argument of the function ...
Read the standard output of a subprocess line by line in Python
How to check the memory size of a dictionary in Python
A function that measures the processing time of a method in python
Check if you can connect to a TCP port in Python
Get the formula in an excel file as a string in Python
The story of making a module that skips mail with python
Get the number of readers of a treatise on Mendeley in Python
Create a compatibility judgment program with the random module of python.
If you think that the person you put in with pip doesn't work → Maybe you are using python3?
[Django] What to do if the model you want to create has a large number of fields
How to write when you want to put a number after the group number to be replaced with a regular expression in re.sub of Python
What to do if you get `locale.Error: unsupported locale setting` when getting the day of the week from a date in Python
Check the behavior of destructor in Python
Write the test in a python docstring
Pass the path of the imported python module
Display a list of alphabets in Python 3
Comparison of Japanese conversion module in Python3
Make a relation diagram of Python module
Check if the URL exists in Python
Run the Python interpreter in a script
The result of installing python in Anaconda
Check the path of the Python imported module
[python] [meta] Is the type of python a type?
The basics of running NoxPlayer in Python
The Python project template I think of.
In search of the fastest FizzBuzz in Python
The story of blackjack A processing (python)