Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #95323

Re: Ensure unwanted names removed in class definition

From Ben Finney <ben+python@benfinney.id.au>
Subject Re: Ensure unwanted names removed in class definition
Date 2015-08-13 08:50 +1000
References <85fv3oj2y6.fsf@benfinney.id.au> <mqfpbu$1ev$1@ger.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.146.1439419869.3627.python-list@python.org> (permalink)

Show all headers | View raw


Peter Otten <__peter__@web.de> writes:

> I would probably use a generator expression. These don't leak names:

That's an unexpected inconsistency between list comprehensions versus
generator expressions, then. Is that documented explicitly in the Python
2 documentation?

> Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
> [GCC 4.8.2] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> class Parrot:
> ...     a = [per for per in "abc"]
> ...     b = list(trans for trans in "def")
> ... 
> >>> Parrot.per
> 'c'
> >>> Parrot.trans
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> AttributeError: class Parrot has no attribute 'trans'

We have a winner! That's elegant, expressive, and has the right
behaviour on both Python 2 and Python 3.

-- 
 \         “I turned to speak to God/About the world's despair; But to |
  `\   make bad matters worse/I found God wasn't there.” —Robert Frost |
_o__)                                                                  |
Ben Finney

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Ensure unwanted names removed in class definition Ben Finney <ben+python@benfinney.id.au> - 2015-08-13 08:50 +1000

csiph-web