By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,035 Members | 1,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,035 IT Pros & Developers. It's quick & easy.

Generic utility class for passing data

P: n/a
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?
Oct 29 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Gordon Airporte <JH*****@fbi.gov> wrote:
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
Oct 29 '05 #2

P: n/a
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 ?

Alex Martelli wrote:
Gordon Airporte <JH*****@fbi.gov> wrote:
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


Oct 29 '05 #3

P: n/a
bo****@gmail.com wrote:
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
rk***@ucsd.edu

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

Oct 29 '05 #4

P: n/a
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


Robert Kern wrote:
bo****@gmail.com wrote:
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
rk***@ucsd.edu

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


Oct 29 '05 #5

P: n/a
>>>>> "Gordon" == Gordon Airporte <JH*****@fbi.gov> writes:

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/Coo.../Recipe/413486
-Chris
Oct 29 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.