473,785 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Perlish dictionary behavior

Mr. Brewer:

I fruitlessly tried your "thing counter", as you can see below.
class AllThingsStartA tZero(dict): .... def getitem (self, key):
.... return dict.get(self, key, 0)
.... thingCounts = AllThingsStartA tZero()
for thing in ('a','b','c','d ','a'): .... thingCounts[thing] += 1
....
Traceback (most recent call last):
File "<interacti ve input>", line 2, in ?
KeyError: 'a' thingCounts

{}

Reason tells me it should work for me using...

PythonWin 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit
(Intel)] on win32.

....as it did for you. The runtime interpreter, however, says
otherwise. Can you see what I've done wrong...I can't?

With thanks in advance, I am,

Respectfully yours,

Fred Allen
Jul 18 '05 #1
1 1535
fa****@leveltwo .com (Fred Allen) wrote in
news:72******** *************** ***@posting.goo gle.com:
Mr. Brewer:

I fruitlessly tried your "thing counter", as you can see below.
class AllThingsStartA tZero(dict): ... def getitem (self, key):
it's __getitem__ note the underlines.
... return dict.get(self, key, 0)
... thingCounts = AllThingsStartA tZero()
for thing in ('a','b','c','d ','a'): ... thingCounts[thing] += 1
...
Traceback (most recent call last):
File "<interacti ve input>", line 2, in ?
KeyError: 'a' thingCounts

{}

Reason tells me it should work for me using...

PythonWin 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit
(Intel)] on win32.

...as it did for you. The runtime interpreter, however, says
otherwise. Can you see what I've done wrong...I can't?

With thanks in advance, I am,

Respectfully yours,

Fred Allen


Jul 18 '05 #2

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

Similar topics

3
1288
by: gabriele renzi | last post by:
Hi gurus and nubys, matz said more than once that the perlish global variables like $/ or $: are going away in future releases. I wonder if is there already a decision, what they will become? I mean, it's easy to think that $DEFAULT_INPUT may become a class variable of IO but what about $1..$n or $: ? More, would it be possible to have some kind of backward compat via an
2
414
by: Chris | last post by:
One nice thing about Perl that is helpful when tallying things up by type is that if you increment a hash key and the key does not exist, Perl puts a one there. So you can have code that does something like this: my %thingCounts; foreach my $thing(<file>) { $thingCounts{$thing}++; }
125
7226
by: Raymond Hettinger | last post by:
I would like to get everyone's thoughts on two new dictionary methods: def count(self, value, qty=1): try: self += qty except KeyError: self = qty def appendlist(self, key, *values): try:
6
1837
by: Talin | last post by:
I want to make a dictionary that acts like a class, in other words, supports inheritance: If you attempt to find a key that isn't present, it searches a "base" dictionary, which in turn searches its base, and so on. Now, I realize its fairly trivial to code something like this using UserDict, but given that classes and modules already have this behavior, is there some built-in type that already does this? (This is for doing nested...
90
10821
by: Christoph Zwerschke | last post by:
Ok, the answer is easy: For historical reasons - built-in sets exist only since Python 2.4. Anyway, I was thinking about whether it would be possible and desirable to change the old behavior in future Python versions and let dict.keys() and dict.values() both return sets instead of lists. If d is a dict, code like: for x in d.keys():
17
2022
by: Matt Kruse | last post by:
Perl's map() function is a powerful shortcut to accomplish many "loop over an array and do X" operations. Below is a quick hack to simulate similar functionality. I've used it a few times and find it to be quite handy. Any thoughts? <script type="text/javascript"> function map(func) { var i,j,o; var results = ;
5
1791
by: Brian Richards | last post by:
I'm experiencing some wierd behavior with a Dictionary<T,U> class using foreach loops, such that the Key returned in the foreach is not contained in the dictionary. code: Dictionary<A, B> dict; ..... foreach(A key in dict.Keys) {
8
3112
by: akameswaran | last post by:
I wrote up a quick little set of tests, I was acutally comparing ways of doing "case" behavior just to get some performance information. Now two of my test cases had almost identical results which was not at all what I expected. Ultimately I realized I don't really know how literals are treated within the interpreter. The two implementations I was looking at were: class caseFunction(object): def __init__(self):
8
1531
by: Almad | last post by:
Hello, I discovered this behaviour in dictionary which I find confusing. In SneakyLang, I've tried to extend dictionary so it visits another class after something is added: class RegisterMap(dict): def __setitem__(self, k, v): dict.__setitem__(self, k,v) self.visit_register_map(self)
0
2311
by: James Mills | last post by:
On Fri, Oct 31, 2008 at 8:49 AM, mark floyd <emfloyd2@gmail.comwrote: Mark, this is correct behavior. You have 3 positional arguments in the function definition. You _must_ aupply _all_ 3 of them. If you wish for b to be optional, then you must give it a default value. def foo(a, b=None, c=None): print a
0
10325
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...
1
10091
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,...
0
9950
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
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
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
5381
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...
1
4053
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
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
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.