Hello.
I would like to have a quick way to create dicts from object, so that a
call to foo['bar'] would return obj.bar.
The following works, but I would prefer to use a built-in way if one
exists. Is there one?
Thanks in advance.
class dictobj(dict):
"""
class dictobj(dict):
A dictionary d with an object attached to it,
which treats d['foo'] as d.obj.foo.
"""
def __init__(self, obj):
self.obj = obj
def __getitem__(sel f, key):
return self.obj.__geta ttribute__(key)
--
Thanos Tsouanas .: My Music: http://www.thanostsouanas.com/ http://thanos.sians.org/ .: Sians Music: http://www.sians.org/
Jul 23 '05
31 2572
Thanos Tsouanas wrote:
I'm not sure what you're getting at, but have you tried this:
class A(object):
def __getitem__(sel f, ky):
return self.__dict__[ky]
for example: class A(object):
def __init__(self,a ,b,c):
self.a = a
self.b = b
self.c = c
def __getitem__(sel f, ky):
return self.__dict__[ky]
a = A(1,2,3) a['a']
1 a['b']
2
Hello.
I would like to have a quick way to create dicts from object, so that a call to foo['bar'] would return obj.bar.
The following works, but I would prefer to use a built-in way if one exists. Is there one?
Thanks in advance.
class dictobj(dict): """ class dictobj(dict): A dictionary d with an object attached to it, which treats d['foo'] as d.obj.foo. """ def __init__(self, obj): self.obj = obj def __getitem__(sel f, key): return self.obj.__geta ttribute__(key)
Thanos Tsouanas wrote:
I'm not sure what you're getting at, but have you tried this:
class A(object):
def __getitem__(sel f, ky):
return self.__dict__[ky]
for example: class A(object):
def __init__(self,a ,b,c):
self.a = a
self.b = b
self.c = c
def __getitem__(sel f, ky):
return self.__dict__[ky]
a = A(1,2,3) a['a']
1 a['b']
2
Hello.
I would like to have a quick way to create dicts from object, so that a call to foo['bar'] would return obj.bar.
The following works, but I would prefer to use a built-in way if one exists. Is there one?
Thanks in advance.
class dictobj(dict): """ class dictobj(dict): A dictionary d with an object attached to it, which treats d['foo'] as d.obj.foo. """ def __init__(self, obj): self.obj = obj def __getitem__(sel f, key): return self.obj.__geta ttribute__(key) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: none |
last post by:
or is it just me?
I am having a problem with using a dictionary as an attribute of a
class. This happens in python 1.5.2 and 2.2.2 which I am accessing
through pythonwin builds 150 and 148 respectively
In the sample code you see that I have class Item and class Dict
class Dict contains a dictionary called items. The items dictionary
will contain instances of Item that are keyed off of the Item name. In
__main__ I create two...
|
by: Alan Silver |
last post by:
Hello,
I have a server running Windows Server 2003, on which two of the web
sites use the MegaBBS ASP forum software. Both sites suddenly developed
the same error, which seems to be connected to the dictionary object.
After some tinkering, I whittled it down to the following (complete)
ASP...
<%@ CodePage=65001 Language="VBScript"%>
|
by: David Rasmussen |
last post by:
If I have a string that contains the name of a function, can I call it?
As in:
def someFunction():
print "Hello"
s = "someFunction"
s() # I know this is wrong, but you get the idea...
/David
|
by: ESPNSTI |
last post by:
Hi,
I'm trying to use a generics dictionary with a key class that implements and
needs IComparable<>.
However when I attempt to use the dictionary, it doesn't appear to use the
IComparable<> to find the key.
In the example below, accessing the dictionary by using the exact key object
that was used to add to the dictionary works.
(see code comment 1).
However, if I attempt to access the dictionary by using a key object that
|
by: jg |
last post by:
I was trying to get custom dictionary class that can store generic or
string; So I started with the example given by the visual studio 2005 c#
online help for simpledictionay object
That seem to miss a few things including #endregion directive and the ending
class }
Is there not a simple way like in dotnet vb?
I managed to get the sample to code to this:
| |
by: Betina Andersen |
last post by:
I have a dictionary object, then I create a new dictionary object and sets
it equal to my original, then I pass the new dictionary object to a function
that changes some of my values - but then my original dictionary also gets
changed and that was not the intention, can someone explain to me why it
behaves that way and how do I avoid it, så I van have different dictionary
objects?
Thanks Betina
|
by: randy1200 |
last post by:
I have the following:
Dictionary<object, stringd = new Dictionary<object, string>();
I can add entries to the dictionary, and I can loop through the entries I've
added to the Dictionary. This all works great.
My question is: I'd like to find a way to get just the first key, without
looping through the whole Dictionary. I tried the code below, but I get a
compile error trying to use an index value on the KeyCollection:
|
by: NullQwerty |
last post by:
Hi folks,
I have a Dictionary which contains a string key and an object value. I
want the object value to point to a property in my class and I want it
to be by reference, so that later on I can change the value of the
property through the dictionary.
I am having difficulty making the value be by reference. Is this
possible? I've even tried unsuccessfully to use unsafe code with
pointers.
|
by: GiJeet |
last post by:
hello, I'm trying to use a dictionary as a class member. I want to
use a property to get/set the key/value of the dictionary but I'm
confused as how to use a dictionary as a property. Since there are 2
parts, I don't know how to setup the get/sets. I tried searching but
could not find any examples. I'd appreciate an example of how to get/
set the two parts of a dictionary via a property of a class.
tia
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |