472,958 Members | 1,783 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Making static dicts?

Hello!

Just to ask, is it possible to make a static dictionary in python. So
that the keys in the dictionary cannot be removed, changed or new ones
added, but the value pairs can.

Is this possible with python?

thanks,

Ognjen.

Jun 18 '07 #1
2 9755
On Jun 18, 1:46 pm, Ognjen Bezanov <Ogn...@mailshack.comwrote:
Hello!

Just to ask, is it possible to make a static dictionary in python. So
that the keys in the dictionary cannot be removed, changed or new ones
added, but the value pairs can.

Is this possible with python?

thanks,

Ognjen.
How much functionality do you need? Something like this might work
(though it could use better error messages.

Expand|Select|Wrap|Line Numbers
  1. class StaticDict:
  2. def __init__(self,srcdict):
  3. self._srcdict = srcdict
  4. def __getitem__(self,idx):
  5. return self._srcdict[idx]
  6.  
Use it like this:
>>sd = StaticDict({'a':'b'})
sd['a']
'b'
>>sd['b']
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 5, in __getitem__
KeyError: 'b'
>>sd['a'] = "hello"
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: StaticDict instance has no attribute '__setitem__'
>>>
Jun 18 '07 #2
On Mon, 18 Jun 2007 21:46:23 +0100, Ognjen Bezanov wrote:
Hello!

Just to ask, is it possible to make a static dictionary in python. So
that the keys in the dictionary cannot be removed, changed or new ones
added, but the value pairs can.

Is this possible with python?

I'm sure it is possible, but you'll have to program it yourself.

The usual term for what you are describing is "immutable" rather than
static. For some ways of making an immutable class, see here:

http://northernplanets.blogspot.com/...in-python.html

To get the dictionary behaviour, the easiest ways would be either to
sub-class from dict:

class ImmutableDict(dict):
pass

or perhaps use delegation (google on "Python automatic delegation" for
more information).

You might like to look at the source code for the UserDict module in the
standard library for some ideas (especially the DictMixin class).

I leave putting these pieces together into a working immutable dictionary
up to you. Good luck!
--
Steven.

Jun 18 '07 #3

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

Similar topics

7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
5
by: ffrugone | last post by:
My scenario involves two classes and a database. I have the classes "Broom" and "Closet". I want to use a static method from the "Closet" class to search the database for a matching "Broom". If...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
4
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...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.