I'm new to Python. How do I create a private constructor which should be called only by the static function of the class and not from else where?
解决方案How do I create a private constructor?
In essence, it's impossible both because python does not use constructors the way you may think it does if you come from other OOP languages and because python does not enforce privacy, it just has a specific syntax to suggest that a given method/property should be considered as private. Let me elaborate...
First: the closest to a constructor that you can find in python is the __new__ method but this is very very seldom used (you normally use __init__, which modify the just created object (in fact it already has self as first parameter).
Regardless, python is based on the assumption everybody is a consenting adult, thus private/public is not enforced as some other language do.
As mentioned by some other responder, methods that are meant to be "private" are normally prepended by either one or two underscores: _private or __private. The difference between the two is that the latter will scramble the name of the method, so you will be unable to call it from outside the object instantiation, while the former doesn't.
So for example if your class A defines both _private(self) and __private(self):
>>> a = A()
>>> a._private() # will work
>>> a.__private() # will raise an exception
You normally want to use the single underscore, as - especially for unit testing - having double underscores can make things very tricky....
HTH!