473,811 Members | 2,771 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
12 1436
>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**********@r eader2.panix.co m>, Roy Smith <ro*@panix.comw rote:
>>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**@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 28 '06 #13

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

Similar topics

12
9009
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
1884
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
9728
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
9605
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
10648
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10389
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...
0
10135
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6890
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4339
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
2
3867
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3018
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.