I agree with Mike, I don't think vars(obj).update(kwargs) is the best way to do 
it. Python trusts the programmer enough to let you bypass the middle layers of 
abstraction like this, but it's not always a good idea.

Besides, it only takes 2 lines to do it with setattr() instead:

    for key, val in kwargs.iteritems():
        setattr(obj, key, val)

SQLAlchemy aside, I think the above is better anyway. To me it's a lot clearer 
at a glance what it is doing, but they may just be me. I've never seen anyone 
do your vars update method.

Cameron Jackson
Engineering Intern
Air Operations
Thales Australia
Thales Australia Centre, WTC Northbank Wharf, Concourse Level,
Siddeley Street, Melbourne, VIC 3005, Australia
Tel: +61 3 8630 4591
cameron.jack...@thalesgroup.com.au<mailto:cameron.jack...@thalesgroup.com.au> | 
www.thalesgroup.com.au<http://www.thalesgroup.com.au>
From: sqlalchemy@googlegroups.com [mailto:sqlalchemy@googlegroups.com] On 
Behalf Of Michael Bayer
Sent: Tuesday, 10 January 2012 10:59 AM
To: sqlalchemy@googlegroups.com
Subject: Re: [sqlalchemy] Strange session.commit behavior


On Jan 7, 2012, at 6:05 PM, Pavel Ponomarev wrote:


Hi,

Heard lots of good things about sqlalchemy and decided to give it a
try.
But almost immediately was confused by strange session.commit()
behavior, please look through following snippets.

Update is pretty straightforward:

\And it works great, but when I need to update bunch of attrs from dict
first thought would be built-in vars function:


obj.attr
bar

kwargs = {'attr':'foo'}
vars(obj).update(kwargs)
obj.attr


SQLAlchemy uses descriptors (see 
http://docs.python.org/reference/datamodel.html#implementing-descriptors) to 
intercept attribute set/get/delete events.  These events then feed into the 
unit of work implementation and result in SQL statements to emit when the 
pending state is flushed.   This usage of descriptors is mentioned in passing 
at 
http://www.sqlalchemy.org/docs/orm/tutorial.html#create-an-instance-of-the-mapped-class

When you use vars(obj), you're essentially dealing with obj.__dict__ directly.  
This bypasses the class in use and any behavior defined on it, essentially 
writing data directly to the underlying storage (arguably not as pythonic, wont 
work with __slots__ for example).   So you need to use setattr() or other 
methods that don't bypass instrumentation when setting attributes.


--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.


-------------------------------------------------------------------------
DISCLAIMER: This e-mail transmission and any documents, files and 
previous e-mail messages attached to it are private and confidential.  
They may contain proprietary or copyright material or information that 
is subject to legal professional privilege.  They are for the use of 
the intended recipient only.  Any unauthorised viewing, use, disclosure, 
copying, alteration, storage or distribution of, or reliance on, this 
message is strictly prohibited.  No part may be reproduced, adapted or 
transmitted without the written permission of the owner.  If you have 
received this transmission in error, or are not an authorised recipient, 
please immediately notify the sender by return email, delete this 
message and all copies from your e-mail system, and destroy any printed 
copies.  Receipt by anyone other than the intended recipient should not 
be deemed a waiver of any privilege or protection.  Thales Australia 
does not warrant or represent that this e-mail or any documents, files 
and previous e-mail messages attached are error or virus free.  

-------------------------------------------------------------------------

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to