Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Closures in leu of pointers?

Reply
Thread Tools

Closures in leu of pointers?

 
 
alex23
Guest
Posts: n/a
 
      07-01-2013
On 30/06/2013 3:46 PM, Ian Kelly wrote:
> In general I agree, although when reading code I would definitely
> prefer if the locals were declared.


If you import the code into the interpreter as an adjunct to reading it
you can see the locals with:

>>> somefunc.func_code.co_varnames # 2.x
>>> somefunc.__code__.co_varnames # 3.x


> On a related note, I think that generator functions should in some way
> be explicitly marked as such in the declaration, rather than needing
> to scan the entire function body for a yield statement to determine
> whether it's a generator or not.


>>> inspect.isgenerator(somefunc)


However, a dedicated code reader built around the inspect module could
be a handy tool.

 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      07-01-2013
On Sat, 29 Jun 2013 23:46:12 -0600, Ian Kelly wrote:

> On a related note, I think that generator functions should in some way
> be explicitly marked as such in the declaration, rather than needing to
> scan the entire function body for a yield statement to determine whether
> it's a generator or not.


That was considered when generators were introduced in Python 2.2.
Guido's rationale for preferring to keep "def" for both generator
functions and normal functions is given in the PEP:


Issue: Introduce another new keyword (say, "gen" or "generator") in
place of "def", or otherwise alter the syntax, to distinguish
generator-functions from non-generator functions.

Con: In practice (how you think about them), generators *are*
functions, but with the twist that they're resumable. The mechanics
of how they're set up is a comparatively minor technical issue, and
introducing a new keyword would unhelpfully overemphasize the
mechanics of how generators get started (a vital but tiny part of a
generator's life).

Pro: In reality (how you think about them), generator-functions are
actually factory functions that produce generator-iterators as if by
magic. In this respect they're radically different from non-generator
functions, acting more like a constructor than a function, so reusing
"def" is at best confusing. A "yield" statement buried in the body is
not enough warning that the semantics are so different.

BDFL: "def" it stays. No argument on either side is totally
convincing, so I have consulted my language designer's intuition. It
tells me that the syntax proposed in the PEP is exactly right - not
too hot, not too cold. But, like the Oracle at Delphi in Greek
mythology, it doesn't tell me why, so I don't have a rebuttal for the
arguments against the PEP syntax. The best I can come up with (apart
from agreeing with the rebuttals ... already made) is "FUD". If this
had been part of the language from day one, I very much doubt it
would have made Andrew Kuchling's "Python Warts" page.


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


5+ versions later, I think that Guido has been shown to be correct. Even
if you believe that generator functions would have been better with
different syntax, there is no evidence that re-using def is actively
harmful.



--
Steven
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Does deleting a container of pointers also delete the (contained) pointers? Xamalek C++ 7 11-04-2003 04:17 PM
c++: pointers to pointers A C++ 3 10-29-2003 01:15 PM
pointers to pointers // exception handling error muser C++ 3 09-18-2003 06:19 PM
Template specialization of pointers with function pointers Phil C++ 1 09-16-2003 02:17 AM



Advertisments