dicts vs classes

  • Thread starter =?iso-8859-1?B?R3V5b24gTW9y6WU=?=
  • Start date
?

=?iso-8859-1?B?R3V5b24gTW9y6WU=?=

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
 
M

Marco Wahl

Guyon Morée said:
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
 
M

Marc 'BlackJack' Rintsch

Guyon Morée said:
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
 
S

Simon Hibbs

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
 
A

Aahz

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 ([email protected]) <*> 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
 
B

bearophileHUGS

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
 
B

bearophileHUGS

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
 
S

Simon Forman

Simon said:
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
 
A

Aahz

Aahz, citing Guido:

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 ([email protected]) <*> 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
 
R

Roy Smith

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".
 
N

Nick Vatamaniuc

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
 
A

Aahz

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 ([email protected]) <*> 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
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top