[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Function Inheritance



Inheritance would be the more "software engineering politically correct scholastic" way of doing this.

However, this is how you accomplish this the way you describe.

def hello():
   print "hello world"

def hello_vars(*args):
   print " ".join(a for a in args)

class A(object):
   pass

class B(object):
   def __init__(self):
      self.usethis = hello_vars

>>>myobj = A()
>>>myobj.say_hello = hello
>>>myobj.print_args = hello_vars

>>>myobj.say_hello()
hello world
>>>myobj.hello_vars("hi", "from", "python")
hi from python

>>>anewobj = B()
>>>anewobj.usethis("hi", "from", "B")
hi from B

-Thadeus




On Wed, Oct 14, 2009 at 4:27 PM, Kris Schnee <kschnee@xxxxxxxxxx> wrote:
This is more of a general Python question.

I've finished testing a class with some functions I like. I want to use those functions in a new class, but not to use the module I just built. (Because I might end up with several such classes.) So I'm thinking of doing this:

-Make a new module.
-Give it a class that starts with no functions defined.
-Write a module containing the tested functions without their being part of a class. Eg.

def DoSomething(self,**options):
 pass

-Import the tested functions as a module called "spam"
-Have the new module's class manually say, "self.DoSomething = spam.DoSomething".

Seems cumbersome, doesn't it? I'm basically trying to give my new class access to the neat functions while putting them into a separate module, so that the main module isn't cluttered by them. Or should I rely on a linear chain of inheritance, so that FancyClass is a subclass of BasicClass from another module?