I have a class factory, very similar to the one here. I want to set a variable that the created class will have access to, when calling createShape. How do you do this pythonicly?
For example:
foo = 'bar'
circle = ShapeFactory.createShape('circle', foo)
print circle.foo >>> bar
however, since createShape is static, I can't add
self.foo = foo
to the createShape method.
Obviously all shapes would implement this parameter.
You can't do it with self
, but you can still do it with the object created in createShape
. That code does this in createShape
:
return ShapeFactory.factories[id].create()
So just do this:
myShape = ShapeFactory.factories[id].create()
myShape.foo = foo
return myShape
However, looking at the example on the page you linked to, I can't say I'd recommend that approach at all. The design described in that example is very awkward.