Generic utility class for passing data

G

Gordon Airporte

I'm wondering if this is might be bad practice. Sometimes when I need to
pass around several pieces of datum I will put them in a tuple, then
when I need to use them in a receiving function I get them out with
subscripts. The problem is that the subscript number is completely
meaningless and I have to remember the order I used.
As an alternative I was considering using a dummy class like this:

class Dummy:
pass

Then when I need to pass some related data, Python lets me do this:

prefill = Dummy()
prefill.foreground = 'blue' #"foreground" is made up on the fly
prefill.background = 'red'
prefill.pattern = mypattern
return prefill

Now I can access the data later using meaningful names.
Is this going to cause problems somehow? Should I rather go to the
trouble of creating more substantial individual classes for every
grouping of data I might need to pass (with __init__'s and default
values and so on)? Should I just stick with subscripted groupings
because of the overhead?
 
A

Alex Martelli

Gordon Airporte said:
I'm wondering if this is might be bad practice. Sometimes when I need to

I hope not, 'cuz I suggested that years ago on the Cookbook (under the
name of Bunch) with several successive refinements.
class Dummy:
pass

Then when I need to pass some related data, Python lets me do this:

prefill = Dummy()
prefill.foreground = 'blue' #"foreground" is made up on the fly
prefill.background = 'red'
prefill.pattern = mypattern
return prefill

Sure, but you can do even better:

class Dummy(object):
def __init__(self, **kwds): self.__dict__ = kwds

prefill = Dummy(foreground='blue', background='red', pattern=mypattern)
return prefill


Alex
 
B

bonono

just curious, since python don't care what object is being passing
around and the receiving function has to 'decode' it(be it tuple or
dict or whatever). What is the advantage of dummy class ?
 
R

Robert Kern

just curious, since python don't care what object is being passing
around and the receiving function has to 'decode' it(be it tuple or
dict or whatever). What is the advantage of dummy class ?

The convenience of the attribute notation.

For the record, my favorite variation is as follows:

class Bunch(dict):
def __init__(self, *args, **kwds):
dict.__init__(self, *args, **kwds)
self.__dict__ = self

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
B

bonono

ok, so that is simply a wrapper of a dict and has nothing to do with
parameter passing and it can be used anyway within the receiving
function(or basically anyway I want to say x.key rather than x[key]).

def f(**kwargs):
x = Bunch(kwargs)
x.some_key_as_attribute = something
 
C

Chris Smith

Gordon> I'm wondering if this is might be bad practice. Sometimes
Gordon> when I need to pass around several pieces of datum I will
Gordon> put them in a tuple, then when I need to use them in a
Gordon> receiving function I get them out with subscripts. The
Gordon> problem is that the subscript number is completely
Gordon> meaningless and I have to remember the order I used. As
Gordon> an alternative I was considering using a dummy class like
Gordon> this:

Gordon> class Dummy: pass

Gordon> Then when I need to pass some related data, Python lets me
Gordon> do this:

Gordon> prefill = Dummy() prefill.foreground = 'blue'
Gordon> #"foreground" is made up on the fly prefill.background =
Gordon> 'red' prefill.pattern = mypattern return prefill

Gordon> Now I can access the data later using meaningful names.
Gordon> Is this going to cause problems somehow? Should I rather
Gordon> go to the trouble of creating more substantial individual
Gordon> classes for every grouping of data I might need to pass
Gordon> (with __init__'s and default values and so on)? Should I
Gordon> just stick with subscripted groupings because of the
Gordon> overhead?

May fortune smile on Zoran Isailovski, whose Enum class is exactly
what you want:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/413486
-Chris
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top