473,508 Members | 3,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dicts vs classes

Hi all,

I'm using simple classes as a container of named values and I'm
instantiating a lot of them in a very short time.

i was wondering if there is any benefit in using dicts instead from a
performance/memory usage point of view?

regards,

Guyon Morée
http://gumuz.looze.net

Jul 25 '06 #1
12 1410
In <11**********************@i3g2000cwc.googlegroups. com>, Guyon Morée
wrote:
I'm using simple classes as a container of named values and I'm
instantiating a lot of them in a very short time.

i was wondering if there is any benefit in using dicts instead from a
performance/memory usage point of view?
If you really have a memory problem read the documentation about
`__slots__`. But I would only consider this if `a lot of` is several 100k
or millions of objects and the memory consumption really is a problem.

Ciao,
Marc 'BlackJack' Rintsch
Jul 25 '06 #2
"Guyon Morée" <gu*********@gmail.comwrites:
I'm using simple classes as a container of named values and I'm
instantiating a lot of them in a very short time.

i was wondering if there is any benefit in using dicts instead from a
performance/memory usage point of view?
I recommend you to measure the time and memory usage
for the two alternatives. That could give you the
answer you want.
HTH
--
Marco Wahl
http://visenso.com
Jul 25 '06 #3
dict is already a class....why another?

Guyon Morée wrote:
Hi all,

I'm using simple classes as a container of named values and I'm
instantiating a lot of them in a very short time.

i was wondering if there is any benefit in using dicts instead from a
performance/memory usage point of view?

regards,

Guyon Morée
http://gumuz.looze.net
Jul 25 '06 #4
I'm wondering about whether to use objects in this way or dictionaries
for a program I'm writing at the moment. It seems to me that unless you
need some of the functionality supplied with dictionaries (len(a),
has_key, etc) then simple objects are a syntacticaly cleaner and more
natural way to express yourself.

Any objctions to this, or pitfalls?

Simon Hibbs

Jul 25 '06 #5
In article <pa****************************@gmx.net>,
Marc 'BlackJack' Rintsch <bj****@gmx.netwrote:
>In <11**********************@i3g2000cwc.googlegroups. com>, Guyon Morée
wrote:
>>
I'm using simple classes as a container of named values and I'm
instantiating a lot of them in a very short time.

i was wondering if there is any benefit in using dicts instead from a
performance/memory usage point of view?

If you really have a memory problem read the documentation about
`__slots__`. But I would only consider this if `a lot of` is several 100k
or millions of objects and the memory consumption really is a problem.
Guido sez:

__slots__ is a terrible hack with nasty, hard-to-fathom side
effects that should only be used by programmers at grandmaster and
wizard levels. Unfortunately it has gained an enormous undeserved
popularity amongst the novices and apprentices, who should know
better than to use this magic incantation casually.
--
Aahz (aa**@pythoncraft.com) <* http://www.pythoncraft.com/

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it." --Brian W. Kernighan
Jul 25 '06 #6
Simon Hibbs:
It seems to me that unless you
need some of the functionality supplied with dictionaries (len(a),
has_key, etc) then simple objects are a syntacticaly cleaner and more
natural way to express yourself.
I'd say the opposite. Classes contain a dict of their attributes, etc.
So if you don't need the functionality supplied by objects, then using
simpler dictionaries is better. (But in the end the choice has to be
made according to the specific situations).

Bye,
bearophile

Jul 25 '06 #7
Aahz, citing Guido:
>__slots__ is a terrible hack with nasty, hard-to-fathom side
effects that should only be used by programmers at grandmaster and
wizard levels. Unfortunately it has gained an enormous undeserved
I think I have used __slots__ just one time. Can you tell me some of of
such bad side effects?

Bye,
bearophile

Jul 25 '06 #8
Simon Hibbs wrote:
I'm wondering about whether to use objects in this way or dictionaries
for a program I'm writing at the moment. It seems to me that unless you
need some of the functionality supplied with dictionaries (len(a),
has_key, etc) then simple objects are a syntacticaly cleaner and more
natural way to express yourself.

Any objctions to this, or pitfalls?

Simon Hibbs
I'm not sure, but I think this should be the other way round: unless
you need special behavior that dicts don't supply (methods) or you
really want/need obj.attr notation, you're better off just using dicts,
but Marco Wahl is right, if it really matters measure it.

Peace,
~Simon

Jul 25 '06 #9
In article <11**********************@i3g2000cwc.googlegroups. com>,
<be************@lycos.comwrote:
>Aahz, citing Guido:
>>
__slots__ is a terrible hack with nasty, hard-to-fathom side
effects that should only be used by programmers at grandmaster and
wizard levels. Unfortunately it has gained an enormous undeserved

I think I have used __slots__ just one time. Can you tell me some of of
such bad side effects?
The main one is that inheritance becomes difficult to nearly-impossible.
--
Aahz (aa**@pythoncraft.com) <* http://www.pythoncraft.com/

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it." --Brian W. Kernighan
Jul 25 '06 #10
>Guido sez:
>
__slots__ is a terrible hack with nasty, hard-to-fathom side
effects that should only be used by programmers at grandmaster and
wizard levels. Unfortunately it has gained an enormous undeserved
popularity amongst the novices and apprentices, who should know
better than to use this magic incantation casually.
But, if they are novices, why should they be expected to know better?

I just re-read http://docs.python.org/ref/slots.html#l2h-217 and don't
see anyplace where it says, "Warning: for use by wizards only".
Jul 25 '06 #11
Don't optimize prematurely. Write whatever is cleaner, simpler and
makes more sense. Such that if someone (or even yourself) looks at it
10 years from now they'll know exactly what is going on. As far as
what is slower or what functionality you will use and what you won't --
well, if you won't use it then why worry about it and to find out if it
is slower run a quick benchmark and you'll know...
-NickV

be************@lycos.com wrote:
Simon Hibbs:
It seems to me that unless you
need some of the functionality supplied with dictionaries (len(a),
has_key, etc) then simple objects are a syntacticaly cleaner and more
natural way to express yourself.

I'd say the opposite. Classes contain a dict of their attributes, etc.
So if you don't need the functionality supplied by objects, then using
simpler dictionaries is better. (But in the end the choice has to be
made according to the specific situations).

Bye,
bearophile
Jul 25 '06 #12
In article <ea**********@reader2.panix.com>, Roy Smith <ro*@panix.comwrote:
>>Guido sez:

__slots__ is a terrible hack with nasty, hard-to-fathom side
effects that should only be used by programmers at grandmaster and
wizard levels. Unfortunately it has gained an enormous undeserved
popularity amongst the novices and apprentices, who should know
better than to use this magic incantation casually.

But, if they are novices, why should they be expected to know better?

I just re-read http://docs.python.org/ref/slots.html#l2h-217 and don't
see anyplace where it says, "Warning: for use by wizards only".
Unfortunately, the Python docs are currently suboptimal when it comes to
new-style classes.
--
Aahz (aa**@pythoncraft.com) <* http://www.pythoncraft.com/

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it." --Brian W. Kernighan
Jul 28 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
8982
by: Afanasiy | last post by:
I have some code like this... self.write( ''' lots of stuff here with %(these)s named expressions ''' % vars(self) ) Then I wanted to add an item to the dict vars(self), so I tried :
1
1186
by: Kwikrick | last post by:
When calling str() on a sequence or dict object, the elements of the sequence/dict will be represented as if their __repr__ method was called. Why is this? Wouldn't it be more consistent when...
9
1618
by: rbt | last post by:
What's a good way to write a dictionary out to a file so that it can be easily read back into a dict later? I've used realines() to read text files into lists... how can I do the same thing with...
6
1824
by: bearophileHUGS | last post by:
I have found that in certain situations ordered dicts are useful. I use an Odict class written in Python by ROwen that I have improved and updated some for personal use. So I'm thinking about a...
12
1844
by: Alan Isaac | last post by:
This discussion ended abruptly, and I'd like to see it reach a conclusion. I will attempt to synthesize Bill and Carsten's proposals. There are two proposed patches. The first is to...
0
965
by: Chris Rebert | last post by:
On Thu, Oct 16, 2008 at 12:19 PM, John Townsend <jtownsen@adobe.comwrote: Right, this clobbers the existing entry with this new blank one. This is evidenced by the fact that you're performing an...
4
1547
by: John Townsend | last post by:
Joe had a good point! Let me describe what problem I'm trying to solve and the list can recommend some suggestions. I have two text files. Each file contains data like this: Test file 1234 4567...
0
7228
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7128
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
7332
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,...
0
7502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1565
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.