[Tutor] How do you share a method (function) among several
objects?
Jacob S.
keridee at jayco.net
Mon Feb 28 00:27:59 CET 2005
Is it possible to put all of those objects in their own module, having the
function you describe as a module defined function? For example pseudo
code....
### Lib.py ###
def function(x):
return stuff
class C1:
def __init__(self):
init stuff
def funct(self,arg):
return function(arg)
class C2:
def __init__(self):
init stuff for this class
def clone(self,arg):
return function(arg)
######################
Usage of this module could be simple enough.
import Lib
a = Lib.C1()
a.funct("a")
b = Lib.C2()
b.clone("a")
The methods defined funct and clone do the same things.
HTH,
Jacob
> Hello
>
> There are several different objects. However, they all share the same
> function.
>
> Since they are not the same or similar, it's not logical to use a
> common superclass.
>
> So I'm asking, what's a good way to allow those objects to share that
> function?
>
> The best solution I've found so far is to put that function in a
> module, and have all objects import and use it. But I doubt that's a
> good use-case for modules; writing and importing a module that contains
> just a single function seems like an abuse.
>
> Thanks,
> Xif
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
More information about the Tutor
mailing list