[prev in list] [next in list] [prev in thread] [next in thread] 

List:       python-dev
Subject:    Re: [Python-Dev] PEP 310 and exceptions
From:       Nick Coghlan <ncoghlan () gmail ! com>
Date:       2005-04-24 23:37:59
Message-ID: 426C2DD7.7070507 () gmail ! com
[Download RAW message or body]

Toby Dickenson wrote:
> On Sunday 24 April 2005 07:42, Nick Coghlan wrote:
> 
>>Shane Hathaway wrote:
> 
> 
>>>While we're on the subject of block handler method names, do the method
>>>names need four underscores?  'enter' and 'exit' look better than
>>>'__enter__' and  '__exit__'.
> 
> 
> I quite like .acquire() and .release(). 
> 
> There are plenty of classes (and not just in the threading module) which 
> already have methods with those names that could controlled by a 'with'. 
> 
> Those names also make the most sense in the C++ 'resource acquisition' model.

Such existing pairings can be easily handled with a utility class like the one 
below. Besides, this part of the naming was considered for the original 
development of PEP 310 - entering and exiting the block is common to _all_ uses 
of the syntax, whereas other names are more specific to particular use cases.

class resource(object):
   def __init__(self, enter, exit):
     self.enter = enter
     self.exit = exit
   def __enter__(self):
     self.enter()
   def __exit__(self):
     self.exit()


with resource(my_resource.acquire, my_resource.release):
   # Do stuff!

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan@gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/python-dev%40progressive-comp.com
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic