473,385 Members | 1,356 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,385 software developers and data experts.

converting dict to object


If I have a dictionary such as:

d = {'a' : 1, 'b' : 2}

is there a way to convert it into an object o, such as:

o.a = 1
o.b = 2

thanks
--
View this message in context: http://www.nabble.com/converting-dic....html#a7649225
Sent from the Python - python-list mailing list archive at Nabble.com.

Dec 2 '06 #1
1 5936
On Fri, 01 Dec 2006 17:48:40 -0800, rieh25 wrote:
If I have a dictionary such as:

d = {'a' : 1, 'b' : 2}

is there a way to convert it into an object o, such as:

o.a = 1
o.b = 2
Rather, the question could be asked the other way around: how can you
convert that object into a dict?

The attributes in 'o' are part of its '__dict__' attribute, which is a
dictionary. That is,

o.a = 1

is equivalent to

o.__dict__['a'] = 1

If you already have a dictionary 'd', you can overlay it onto the object's
attributes with

o.__dict__.update(d)

Carl
Dec 2 '06 #2

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

Similar topics

3
by: Hari Menon | last post by:
Hi, Is there a clean way to build up an XML Node from an object? What I am presently doing to create the node is: 1. Create a StringWriter and then use XmlSerializer to serialize the object...
1
by: James X. Li | last post by:
Hi everyone, the class XmlSerializer provides a generic way to convert a C# object to a XML file. Does anybody know a similar way to convert a C# object to a XmlDocument or XmlElement object...
5
by: Mark Rae | last post by:
Hi, Can anyone please tell me how to convert an object say, a System.Web.Mail.MailMessage object, to a byte array and then convert the byte array to a Base64 string? Any assistance gratefully...
2
by: tlk | last post by:
Hi, everyone. I'm having a problem that I hope some more experienced developers can help me figure-out. We have a project called Records that has been around for a year or so. We're currently...
12
by: jeremito | last post by:
Please excuse me if this is obvious to others, but I can't figure it out. I am subclassing dict, but want to prevent direct changing of some key/value pairs. For this I thought I should override...
2
by: MikalE | last post by:
Hi, I'll try and explain my problem. I'm simulating an incoming event that in my application I get from a COM object. My problem is that I can't seem to figure out how to convert an object back...
1
by: Matt | last post by:
Hi all, So a lot of digging on doing this and still not a fabulous solution: import time # this takes the last_modified_date naive datetime, converts it to a # UTC timetuple, converts that...
3
by: Doogie | last post by:
Hi I have a gridview on my web page and I bind it to an object of my own creation. When I click a button on that page, I want to get the data from that gridview and cast it back into that object...
0
by: =?Utf-8?B?ZGVlcGFr?= | last post by:
Hi All, I have a httpwebresponse object(given below) Dim req As WebRequest Dim rsp As WebResponse <....logic to read the http web request object.....> rsp = req.GetResponse()
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.