You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@thrift.apache.org by Phillip B Oldham <ph...@gmail.com> on 2009/09/03 12:53:35 UTC

[Python] enabling the `with` statement?

Are there plans to enable use of the python `with` statement?

For example, it would be useful to be able to do something like the
following (server implementation):

with MyThriftObject.containedObject as obj:
    myvariable = obj.foo
    # do something here

or:

obj = MyThriftObject()
obj.containedObjects = []
for my_dict in my_list_of_dicts:
    with ContainedObject() as c:
        # map my_dict to c here
        obj.containedObjects.append(c)

Currently python throws errors such as:
  AttributeError: 'MyThriftObject' object has no attribute '__exit__'

-- 
Phillip B Oldham
phillip.oldham@gmail.com
+44 (0) 7525 01 09 01

Re: [Python] enabling the `with` statement?

Posted by David Reiss <dr...@facebook.com>.
Just use a local variable for obj or c.  It looks like you are
trying to use Visual Basic's "with" statement.  Python's is closer to
C#'s "using".

http://www.python.org/dev/peps/pep-0343/

Phillip B Oldham wrote:
> Are there plans to enable use of the python `with` statement?
> 
> For example, it would be useful to be able to do something like the
> following (server implementation):
> 
> with MyThriftObject.containedObject as obj:
>     myvariable = obj.foo
>     # do something here
> 
> or:
> 
> obj = MyThriftObject()
> obj.containedObjects = []
> for my_dict in my_list_of_dicts:
>     with ContainedObject() as c:
>         # map my_dict to c here
>         obj.containedObjects.append(c)
> 
> Currently python throws errors such as:
>   AttributeError: 'MyThriftObject' object has no attribute '__exit__'
>