I'm following this Python Design pattern, and there is something I don't understand about the initialization() function:
class ObjectFactory:
""" Manages prototypes.
Static factory, that encapsulates prototype
initialization and then allows instatiation
of the classes from these prototypes.
"""
__type1Value1 = None
__type1Value2 = None
__type2Value1 = None
__type2Value2 = None
@staticmethod
def initialize():
ObjectFactory.__type1Value1 = Type1(1)
ObjectFactory.__type1Value2 = Type1(2)
ObjectFactory.__type2Value1 = Type2(1)
ObjectFactory.__type2Value2 = Type2(2)
Why the init for the variables are using the name of the class (i.e. ObjectFactory.__type1Value1
) and not using self (i.e. self.__type1Value1
)?
When I changed to self:
def initialize(self):
self.__type1Value1 = Type1(1)
self.__type1Value2 = Type1(2)
self.__type2Value1 = Type2(1)
self.__type2Value2 = Type2(2)
I got error TypeError: initialize() missing 1 required positional argument: 'self'
.
But in another example, using "self" worked:
class Geek:
# Variable defined inside the class.
inVar = 'inside_class'
print("Inside_class2", inVar)
def access_method(self):
self.inVar="a"
print("Inside_class3", self.inVar)
uac = Geek()
uac.access_method()
Output:
Inside_class2 inside_class
Inside_class3 a
What am I missing?