473,403 Members | 2,323 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,403 software developers and data experts.

RIIA in Python 2.5 alpha: "with... as"

Hello,

Having heard that Python 2.5 offers some kind of RIIA concept via
PEP343, got it downloaded (Windows version) and tried. But it did not
work as expected and as wanted.
For the time since I first learned Python, the only reason why I just
could not use it was inability to localize the lifetime of some
variables inside some syntactical "blocks", especially ones in
"for"/"while"/"if" statements, but possibly in any "manually
generated" block (say, like if I would create a "{}" block in C/C++
just to separate one part of the function from another). I mean the
cases like
for k in a1:
pass
print "k: %s" % k
where "k" lives long after the actual need in it was lost, and even
list comprehensions:
b1 = [l for l in a1]
print "l: %s" % l
..

So, with 2.5, I tried to utilize "with...as" construct for this, but
unsuccessfully:
from __future__ import with_statement
with 5 as k:
pass
print k
- told me that "AttributeError: 'int' object has no attribute
'__context__'".
So, does this mean that we still don't have any kind of RIIA in
Python, any capability to localize the lifetime of variables on a
level less than a function, and this is indeed not gonna happen to
change yet?
--
With best regards,
Alexander mailto:ma***************@sinn.ru
Apr 11 '06 #1
5 1851
Alexander Myodov wrote:
So, with 2.5, I tried to utilize "with...as" construct for this, but
unsuccessfully:
from __future__ import with_statement
with 5 as k:
pass
print k
- told me that "AttributeError: 'int' object has no attribute
'__context__'".
So, does this mean that we still don't have any kind of RIIA in
Python, any capability to localize the lifetime of variables on a
level less than a function, and this is indeed not gonna happen to
change yet?


No, it means that Python 2.5 supports 'resource initialisation is
acquisition', but that has nothing to do with the restricting the lifetime
of a variable. You have to use a context manager to handle the resource, 5
isn't a context manager. Some objects which actually need handling as a
resource can be used as context managers, for others you might need to
write your own.

with open('/etc/passwd', 'r') as f:
for line in f:
print line

after executing this f has been closed, but the variable f still exists.

Or try this:
@contextlib.contextmanager .... def silly(n):
.... print "starting to use", n
.... yield n
.... print "finished with", n
.... with silly(5) as k: .... print "hello"
....
starting to use 5
hello
finished with 5 k

5

The resource is controlled by the with statement, but the scope of the
variable and the lifetime of the object are separate issues.
Apr 11 '06 #2
Hello Duncan,

You wrote:
Alexander Myodov wrote:
So, with 2.5, I tried to utilize "with...as" construct for this, but
unsuccessfully:
...
So, does this mean that we still don't have any kind of RIIA in
Python, any capability to localize the lifetime of variables on a
level less than a function, and this is indeed not gonna happen to
change yet?
No, it means that Python 2.5 supports 'resource initialisation is
acquisition', but that has nothing to do with the restricting the lifetime
of a variable.

Sorry, I misworded the question - RIIA is indeed present at least by
the reason that the examples from PEP pass. Agree, my problem is a bit
different, and I a bit mixed up initialization/acquisition with
lifetime blocks. So, seems that we indeed have one and still don't
have another.
Or maybe you have an idea how this can be fixed? The
simplest way I see is putting all the "controlled" variables into a
dedicated class... and do that each time for each block of variables I
need control lifetime. Is there any simpler way?

--
With best regards,
Alexander mailto:ma***************@sinn.ru
Apr 11 '06 #3
>> No, it means that Python 2.5 supports 'resource initialisation is
acquisition', but that has nothing to do with the restricting the
lifetime of a variable.

Sorry, I misworded the question - RIIA is indeed present at least by
the reason that the examples from PEP pass. Agree, my problem is a bit
different, and I a bit mixed up initialization/acquisition with
lifetime blocks. So, seems that we indeed have one and still don't
have another.
Or maybe you have an idea how this can be fixed? The
simplest way I see is putting all the "controlled" variables into a
dedicated class... and do that each time for each block of variables I
need control lifetime. Is there any simpler way?


Wouldn't a small surrounding function suffice? Something like this
(untested):
def whatever():
def anon():
with open('/etc/passwd', 'r') as f:
for line in f:
print line

Sure, not the nicest of all solutions. But if you really fear that f is
reused, it might help.

Diez
Apr 11 '06 #4
Alexander Myodov wrote:
Sorry, I misworded the question - RIIA is indeed present at least by
the reason that the examples from PEP pass. Agree, my problem is a bit
different, and I a bit mixed up initialization/acquisition with
lifetime blocks. So, seems that we indeed have one and still don't
have another.
Or maybe you have an idea how this can be fixed?


Why do you want to restrict the lifetime of a variable?

If you want the variable to become unassigned, just invoke the del
statement.

Regards,
Martin
Apr 11 '06 #5
Alexander Myodov wrote:
Or maybe you have an idea how this can be fixed? The
simplest way I see is putting all the "controlled" variables into a
dedicated class... and do that each time for each block of variables I
need control lifetime. Is there any simpler way?


I wouldn't use the word "fixed", but I suppose if you must you could do
something like:
import contextlib
@contextlib.contextmanager def controlled(**kw):
class Bunch: pass
obj = Bunch()
obj.__dict__.update(kw)
yield obj
obj.__dict__ = {}

with controlled(a=5, b=[1, 2, 3]) as k: print k.a, k.b, dir(k)
5 [1, 2, 3] ['__doc__', '__module__', 'a', 'b'] print dir(k)

['__doc__', '__module__']

So the lifetime of the variable is still not limited, but the lifetime of
its attributes is. I still don't see what that buys you though.
Apr 11 '06 #6

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

Similar topics

7
by: google12 | last post by:
hi, May anyone help me? I need a lib which support .jpeg file with RGBA mode. PIL seems to support RGB and CMYK mode only... Thanks for your help.
49
by: Ville Vainio | last post by:
I don't know if you have seen this before, but here goes: http://text.userlinux.com/white_paper.html There is a jab at Python, though, mentioning that Ruby is more "refined". -- Ville...
10
by: Berthold Hoellmann | last post by:
Hello, When I use ./configure --with-thread --with-fpectl --with-signal-module \ --with-pymalloc --enable-shared --with-cxx=g++ make test on 2.3.3 I get
25
by: samjnaa | last post by:
Please check for sanity and approve for posting at python-dev. In Visual Basic there is the keyword "with" which allows an object- name to be declared as governing the following statements. For...
1
by: walterbyrd | last post by:
I understand that Python has them, but PHP doesn't. I think that is because mod_php is built into apache, but mod_python is not usually in apache. If mod_python was built into apache, would...
2
by: chardish | last post by:
Hello, I'm trying to find out in a script where the location of the current python is. (I'm writing an installer script in python for a simple server application, so i'm going to do a...
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: 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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...
0
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
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...

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.