Python: Pitfalls of creating a super object
We have found that we have a set of attributes(all are not going to change and pulled from the environment at runtime) exist in majority of our classes. I tried searching around for this but unfortunately "object" is a very common word.
What type of issues will I cause if I do the following:
class MasterObjectv2(object):
UNIQUE_KEY = getUniqueKey()
#other properties for all objects in our system
开发者_开发知识库
or is this better:
class MasterObject(object):
def __init__(self):
object.__init__(self)
#other properties for all objects in our system
self.getUniqueKey()
or option 3
# is that its a really dumb idea to do this.
I was thinking about doing the first because I don't have to worry about someone not calling init on MasterObject.
Obviously our class would change from
class Test(object):
def __init__(self):
self.UNIQUE_KEY = getUniqueKey()
to
class Test(MasterObject):
def __init__(self): pass
Edit
These answers dont answer my question. I am not asking what I should do, I am asking about what are the side effects/things to think about if we were going to go down this path.
I am considering using an environment.* type class but I first want to know what the side effects might be* if I go down the other path.
I don't want this to turn into a composition versus inheritance debate... ;)
With python you can easily control object construction in addition to object initialization; for example defining
class Master(object):
def __new__(klass, *args, **kwargs):
self = object.__new__(klass)
self.attribute1 = ...
self.attribute2 = ...
return self
The code in __new__
is executed before the __init__
eventually defined in the derived class is started (so it doesn't matter if derived class doesn't call Master.__init__(self)
). You can use __new__
to setup some attributes in the instance before the start of __init__
.
Whether or not this is a reasonable thing to do in your case largely depends on details that you did not provide.
Do you really need those as attributes in your actual classes? Sounds like keeping them in some "configuration object" and just accessing it from the classes is a more robust idea.
Prefer to keep inheritance strictly for cases where an is-a relationship is implied, which in your case isn't true, AFAIU.
Use option #1 if the attributes are static. Since the attribute is a property of the class and doesn't change from instance to instance, this seems the right choice. You will not cause any issues by going with option #1 (assuming your classes were implemented similar to Test).
class MasterObjectv2(object):
UNIQUE_KEY = getUniqueKey()
#other properties for all objects in our system
Then your Test class could be implemented as follows:
class Test(MasterObject):
pass
Will the values of the attributes, once taken from the environment, be the same for all those classes? If so, you'd probably be better off just keeping a dict
of those environmental attributes at module level. (Or, as someone else suggested, use a module to populate and store the environmental variables.)
If not, my suggestion would be to use create a class method that you would use to populate a dictionary of the environmental variables in your classes the first time __init__
is called for each of them.
精彩评论