3

I'm trying to provide framework which allows people to write their own plugins. These plugins are basically derived classes. My base class needs some variables to initialize, how can I initialize my base class without having to let my derived class feed the variable in the base class initialization?

#!/bin/python
class BaseClass():
 def __init__(self,config):
 self.config=config
 def showConfig(self):
 print "I am using %s" % self.config
class UserPlugin(BaseClass):
 def __init__(self,config):
 BaseClass.__init__(self,config)
 def doSomething(self):
 print "Something"
fubar = UserPlugin('/tmp/config.cfg')
fubar.showConfig()

My goal is to avoid the need to define the config parameter in the UserPlugin class, since this is something I don't want the user who writes a plugin to be bothered with.

asked Mar 19, 2012 at 23:42
4
  • I don't understand your question. I get that when a UserPlugin is instantiated, you don't want the plugin creator to have to pass a config path. But where do you want the config path to come from, then? Do you want to have a default config path? Also, initializing your base class only happens when you instantiate your base class. When you instantiate a UserPlugin, you call BaseClass.__init__() on it, but you are initializing a UserPlugin instance. Commented Mar 19, 2012 at 23:58
  • Thanks for rephrasing my question because that's exactly it. I understand that BaseClass is initiated when UserPlugin is initiated. Bottom line is that I want a clean way for UserPlugin to inherit methods from BaseClass which requires a variable to initiate without passing that variable to the derived class. What I suppose is also possible is to forget about inheritance and to initiate the baseclass first and then initiate the UserPlugin inside the BaseClass, but I find that a bit ugly. Commented Mar 20, 2012 at 9:39
  • But you still didn't answer my question. Where do you want the config path to come from? I took a wild guess, though. Commented Mar 20, 2012 at 12:39
  • The config path can come from anywhere as long as it doesn't have to be passed through the UserPlugin class. Commented Mar 20, 2012 at 13:00

2 Answers 2

2

You can use argument lists to pass any remaining arguments to the base class:

class UserPlugin(BaseClass):
 def __init__(self, *args, **kwargs):
 BaseClass.__init__(self, *args, **kwargs)
answered Mar 19, 2012 at 23:45
Sign up to request clarification or add additional context in comments.

1 Comment

yes, I understand, but that doesn't differ that much from the posted code and it is something which I would like to avoid somehow. I guess I'll have to forget about class inheritance and initiate the plugin class inside the initiated baseclass or something along that line. Thanks for the info though.
1

Based on your Pastebin code, how about this? This avoids using a separate global, instead using a class attribute, which is accessible as a member to all derived classes and their instances.

#!/bin/python
class BaseClass():
 config = '/tmp/config.cfg'
 def __init__(self):
 pass
 def showConfig(self):
 print "I am using %s" % self.config
class UserPlugin(BaseClass):
 def __init__(self):
 BaseClass.__init__(self)
 def doSomething(self):
 print "Something"
fubar = UserPlugin()
fubar.showConfig()

This was the other way to do it that I mentioned before. Keep in mind that if you want to change the value of BaseClass.config itself, you should access it directly (i.e. BaseClass.config = '/foo/path'; otherwise, you wind up creating a custom UPinstance.config value, leaving BaseClass.config unchanged.

answered Mar 20, 2012 at 12:37

2 Comments

I do understand how objects and inheritance work. My actual goal is to keep the UserPlugin class free from anything required to initialize the base class correctly. This sounds silly and in a way it is, but I want to keep "requirements" to make a derived class as low as possible. It's rather an organisational question than a syntax question. Meanwhile I came to the idea of making globals of the variables the BaseClass requires to initialize it. see here pastebin.com/KfdVYbPz Globals are not ideal and should be avoided if possible, but I think in my case it's the best option.
@jay_t, I don't think it is your best option. You can just store it as a class attribute. See above.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.