473,387 Members | 1,453 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Generic utility class for passing data

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
5 2190
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
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
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
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
>>>>> "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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Diffident | last post by:
Hello All, I have written a webform which is by default derived from "Page" class. I have coded another utility class with few methods and an object of this class is instantiated from the webfom...
19
by: Celine | last post by:
I have a set of similar classes (c1, c2, ...). I would like to create a template (or a page with as much abstraction as possible) to copy/paste, and create ASPX pages to edit (add/update/...)...
3
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
1
by: Néstor Sánchez A. | last post by:
Hi, is there a way, maybe using reflection, to use a generic class passing the type parameter dynamicly (not kwnowing the exact type at compile time)? I tried the next example, but doesn't work: ...
5
by: Torben Laursen | last post by:
I am writing a COM in C# using visual studio 2005 and VSTO. Inside the code I use some support classes that are generic but they are not used in the inferface of the COM. However I still get a...
10
by: fig000 | last post by:
HI, I'm new to generics. I've written a simple class to which I'm passing a generic list. I'm able to pass the list and even pass the type of the list so I can use it to traverse it. It's a...
6
by: Jon | last post by:
Hi, When I try to compile the following generic class, the compiler gives me many errors "Cannot conver type '...' to '...'" on the lines indicated. Besides, the C# compiler gives me errors even...
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
2
by: Andrus | last post by:
Winforms UI assembly has static FormManager.FormCreator method which creates forms taking entity as parameter. I need to pass this method to business objects in business assembly so that business...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.