473,499 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

from dict to member vars...?

Hello,

Lets say I have a class instance with the following member vars: var1, x,
size. Now lets say I have dict with the following keys: var1, x, size.
Is there an easy way to *automatically* assign all the values in the dict
to corresponding (member) vars of the same name as the dict keys?

Thanks for the help.

Jul 18 '05 #1
3 1241
Christopher J. Bottaro wrote:
Hello,

Lets say I have a class instance with the following member vars: var1, x,
size. Now lets say I have dict with the following keys: var1, x, size.
Is there an easy way to *automatically* assign all the values in the dict
to corresponding (member) vars of the same name as the dict keys?


d = dict(...)

for key, val in d.items():
setattr(o, key, val)

--
Regards,

Diez B. Roggisch
Jul 18 '05 #2
Christopher J. Bottaro wrote:
Lets say I have a class instance with the following member vars: var1, x,
size. Now lets say I have dict with the following keys: var1, x, size.
Is there an easy way to *automatically* assign all the values in the dict
to corresponding (member) vars of the same name as the dict keys?

Thanks for the help.


If class is a new style class, you can do it this way:

instance.__dict__.update(some_dictionary)

-- george
Jul 18 '05 #3
George Yoshida <ml@dynkin.com> wrote:
Christopher J. Bottaro wrote:
Lets say I have a class instance with the following member vars: var1, x,
size. Now lets say I have dict with the following keys: var1, x, size.
Is there an easy way to *automatically* assign all the values in the dict
to corresponding (member) vars of the same name as the dict keys?

Thanks for the help.


If class is a new style class, you can do it this way:

instance.__dict__.update(some_dictionary)


You can do this for instances of either classic and newstyle classes,
_except_ newstyle classes which define __slots__ ...
Alex
Jul 18 '05 #4

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

Similar topics

2
2970
by: Steve | last post by:
Hi, I have a very long string, someting like: DISPLAY=localhost:0.0,FORT_BUFFERED=true, F_ERROPT1=271\,271\,2\,1\,2\,2\,2\,2,G03BASIS=/opt/g03b05/g03/basis,...
11
25709
by: cjl | last post by:
Hey all: I want to convert strings (ex. '3', '32') to strings with left padded zeroes (ex. '003', '032'), so I tried this: string1 = '32' string2 = "%03s" % (string1) print string2 >32
31
2530
by: Thanos Tsouanas | last post by:
Hello. I would like to have a quick way to create dicts from object, so that a call to foo would return obj.bar. The following works, but I would prefer to use a built-in way if one exists. ...
5
1697
by: linuxnow | last post by:
I don't know what I'm doing wrong, but the result is that _import_ only works from a python shell, but not when I call a python script. $ cat test.py mod = __import__("/home/me/tests/test_imp")...
7
2385
by: wardm | last post by:
I have created a Dict object in a C++ App that calls (embedded) Python functions. The Dict is to be used to pass variable data between the C++ App and the python functions. However I cannot get...
3
1805
by: Gal Diskin | last post by:
Following a discussion with an associate at work about various ways to build strings from variables in python, I'd like to hear your opinions and preferred methods. The methods we discussed are:...
7
2300
by: Charles D Hixson | last post by:
I'm sure I've read before about how to construct prototypes in Python, but I haven't been able to track it down (or figure it out). What I basically want is a kind of class that has both class...
6
1800
by: daohuy.hua | last post by:
The context is that I have a C# class named MainModel which has a private Dictionary<string, FileStreammember named dict. I also have a property Dict to access to this member: public...
9
1970
by: David | last post by:
With a non-server app there is one instance of the program running and one user 'using' it at a time. With this scenario I'm pretty comfortable with variable scope and lifetime. With a server app...
0
7131
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,...
0
7007
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...
0
7220
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...
1
6894
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...
0
7388
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...
0
5470
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,...
1
4919
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...
0
4600
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...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.