Sharing a method (function) among several objects.

X

xifxif

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
 
D

Diez B. Roggisch

Since they are not the same or similar, it's not logical to use a
common superclass.

As python supports multiple inheritance, it's very well "logical" to do it
by subclassing.
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.

A module is a unit of code that (should) encapsulate a certain
functionality. So it's perfect for your needs. There is no law or even rule
of thumb that makes claims about module size (or the lack of, for this
matter). So create a module - it doesn't cost you anything.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top