473,320 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Enjoying Inheritance and operator Overloading

I've been programming in python for a few months now - and returning
to programming after a gap of about ten years I've really enjoyed
learning python.

I've just made my first forays into inheritance and operator
overloading (both concepts that I initially found hard to grasp).

I've written a simple config file parser - and I thought I'd
experiment with making the interface easier by subclassing dict and
overloading the __setitem__, __getitem__ and __delitem__ methods......

The thing is I'm not 100% certain that what I've done is good python -
or even valid python.

As an example (my config parser is a bit more complex) I've shown my
technique for creating a 'case insensitive' dictionary... and would
welcome comments.

class lowerDict(dict):
"""A case insensitve dictionary. It converts the key to lowercase
when
'getting', 'setting' or 'deleting' an item.
May not work with pop or other methods."""
def __init__(self):
dict.__init__(self)

def __setitem__(self, item, value): # setting a
keyword
item = item.lower()
dict.__setitem__(self, item, value)

def __getitem__(self, item):
"""To implement lowercase keys."""
key = item.lower()
return dict.__getitem__(self, key)

def __delitem__(self, item): # deleting a keyword
key = item.lower()
if not self.has_key(key):
raise KeyError(item)
dict.__delitem__(key)
if __name__ == '__main__':
a = lowerDict()
a['HELLO'] =3
print a
Jul 18 '05 #1
1 2010
>
def __delitem__(self, item): # deleting a keyword
key = item.lower()
if not self.has_key(key):
raise KeyError(item)
dict.__delitem__(key)
Oops - except dict.__delitem__ ought have both self and item as arguments....

Regards,
Fuzzy

http://www.voidspace.org.uk/atlantib...thonutils.html


if __name__ == '__main__':
a = lowerDict()
a['HELLO'] =3
print a

Jul 18 '05 #2

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

Similar topics

2
by: cppsks | last post by:
Here is the code that I am working with: #include <new> #include <iostream> #include "stdlib.h" class Base1 { public: Base1() { cout << "Base1:" << this << endl; }
0
by: cppsks | last post by:
Hello. I posted a question regarding this yesterday. I came up with the following solution but I am a little hesistant as to this solution having any side-effects that I am not aware of. The...
6
by: woosu | last post by:
Hello ladies and gentlemen. I have a relatively simple problem that I've been unable to solve. In the interest of learning C++, I've decided to write a simple game. The basis for the game is...
6
by: apm | last post by:
Recently I have had to use a value type for a complex structure because I don't know how to override the = operator. Can the operator ever be overloaded? Or can inheritance be used with value types?
6
by: Massimo Soricetti | last post by:
Hello, recently I wrote a little class which has to wrap two different type of data, showing the same external interface. I used operator overloading, but the same result I could eventually...
2
by: Tom Smith | last post by:
I'm having difficulty with overloading ==, and it's making my brain melt - can you help...? What I want to have is: 1) A base class A with virtual operator== defined (no problem) 2) A class B...
0
by: erik.erikson | last post by:
I am getting a compiler error that I can't well explain or even understand the origin of (though I boiled it down close...). Below is a bare-bones example. What I am doing is defining the...
8
by: Matthias Pfeifer | last post by:
Hi there, i am implementing a Point and Vector class and found that they have some things in common. Therefore i introduced a common parent class called ndimobj, that hosts an array of n values,...
3
by: puzzlecracker | last post by:
Are static method inheritable under csharp rules, if so how can they be used? Can they be virtual? They reason I am asking is because operators are static, and I don't understand how can we...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.