473,799 Members | 3,026 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 1434
In <11************ **********@i3g2 000cwc.googlegr oups.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*********@gm ail.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.net wrote:
>In <11************ **********@i3g2 000cwc.googlegr oups.com>, Guyon Morée
wrote:
>>
I'm using simple classes as a container of named values and I'm
instantiatin g 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**@pythoncra ft.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************ **********@i3g2 000cwc.googlegr oups.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**@pythoncra ft.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

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

Similar topics

12
9007
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
1202
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 calling str() on sequence to use the __str__ method of the elements in the sequence? As it is now, I often find myself typing something like: print map(str, alist) instead of just
9
1635
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 dicts? Here's some sample output that I'd like to write to file and then read back into a dict: {'.\\sync_pics.py': 1135900993, '.\\file_history.txt': 1135900994, '.\\New Text Document.txt': 1135900552}
6
1839
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 possible C version of Odict (maybe fit for the collections module). On a 32 bit Win Python 2.5 requires about: 28.2 bytes/element for set(int) 36.2 bytes/element for dict(int:None)
12
1883
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 http://docs.python.org/lib/typesmapping.html where it is proposed for footnote (3) to state: Keys and values are listed in an arbitrary order. This order is indeterminate and generally depends on factors outside the scope
0
991
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 _assignment_ on a dictionary key rather than calling a _mutator_ method on a dictionary value. A dictionary has only one value for a given key (but importantly, that value can be a list). Switch to a Dict of Lists of Dicts and append to the...
4
1562
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 8975 I want to compare the numbers in each text file. The data set (i.e. the numbers) has a unique identifier: the "test" + the "file". The text files are similar, but may not be exactly the same. My initial idea was to read the text files and...
0
9688
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9546
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10268
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10247
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7571
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5467
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5593
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4146
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 we have to send another system
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.