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


Groups > comp.lang.python > #39338

Re: Data Tree urgent help!!!!!!

From Peter Otten <__peter__@web.de>
Subject Re: Data Tree urgent help!!!!!!
Date 2013-02-20 11:46 +0100
Organization None
References <4abbe91f-a57e-47de-9cf3-5b9574201c83@googlegroups.com> <mailman.2018.1361277272.2939.python-list@python.org> <aojm6oFu09lU1@mid.individual.net>
Newsgroups comp.lang.python
Message-ID <mailman.2100.1361357186.2939.python-list@python.org> (permalink)

Show all headers | View raw


Leo Breebaart wrote:

> Peter Otten <__peter__@web.de> writes:
> 
>> >>> class Name(str):
>> ...     def __repr__(self):
>> ...             return self
>> ...
>> >>> apple, pear, dog, cat, fork, spoon = map(Name, "apple pear dog cat
>> >>> fork spoon".split())
> 
> Is there any reason why you introduced the Name class? In Python
> 2.7 this works equally well if I just do:
> 
>>>> apple, pear, dog, cat, fork, spoon = map(str, "apple pear dog cat fork
>>>> spoon".split())
> 
> So I was wondering why you used Name.

It was more for fun than profit ;) The OP gave  

[apple, dog, fork]

in his examples, and the "normal" no-nonsense approach using a list of 
strings would produce

['apple', 'dog', 'fork']

I was tempted to carry this even further with

>>> class Name(str):
...     def __repr__(self): return self
... 
>>> class Namespace(dict):
...     def __missing__(self, key):
...             self[key] = result = Name(key)
...             return result
... 
>>> fruit, pets, cutlery = eval("[apple, pear], [dog, cat], [fork, spoon]", 
Namespace())
>>> fruit
[apple, pear]

but resisted until now...

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


Thread

Data Tree urgent help!!!!!! anadionisio257@gmail.com - 2013-02-19 04:14 -0800
  Re: Data Tree urgent help!!!!!! Peter Otten <__peter__@web.de> - 2013-02-19 13:34 +0100
    Re: Data Tree urgent help!!!!!! Ana Dionísio <anadionisio257@gmail.com> - 2013-02-19 04:50 -0800
    Re: Data Tree urgent help!!!!!! Ana Dionísio <anadionisio257@gmail.com> - 2013-02-19 04:50 -0800
    Re: Data Tree urgent help!!!!!! Leo Breebaart <leo@lspace.org> - 2013-02-20 10:18 +0000
      Re: Data Tree urgent help!!!!!! Peter Otten <__peter__@web.de> - 2013-02-20 11:46 +0100
      Re: Data Tree urgent help!!!!!! Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-20 22:01 +1100
        Re: Data Tree urgent help!!!!!! Chris Angelico <rosuav@gmail.com> - 2013-02-20 22:16 +1100
          Re: Data Tree urgent help!!!!!! Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-20 22:47 +1100

csiph-web