Suppose I define a class:
class Foo(object):
#
children = []
#
def __init__(self, *args, **kw):
if kw:
self.__dict__.update(kw)
if args:
self.chidlren = list(args)
In current syntax I have to write the folowing "initialization" code:
foo = \
Foo(
Foo(
Foo(x=3,y=4),
Foo(x=5,y=6),
x=4, y=5
),
x=1, y=2
)
I can't write this code as follows (it seems more natural for me):
foo = \
Foo(
x=1, y=2,
…
[View More] Foo(
x=4, y=5,
Foo(x=3,y=4),
Foo(x=5,y=6))
)
)
Would be desirable to allow two equivalent forms of calling syntax in python:
<caller>(<positional_arguments>, <keyword_arguments>)
and
<caller>(<keyword_arguments>, <positional_arguments>)
?
Best regards,
Zaur
[View Less]
Hello
There's a pattern I am doing all the time: filtering out some elements of a
list, and cleaning them in the same move.
For example, if I have a multi line text, where I want to:
- keep non empty lines
- clean non empty lines
I am doing:
>>> text = """
... this is a multi-line text\t
...
... \t\twith
...
... muliple lines."""
>>> [l.strip() for l in text.split('\n') if l.strip() != '']
['this is a multi-line text', 'with', 'muliple …
[View More]lines.']
It is not optimal, because I call strip() twice. I could use ifilter then
imap or even use a real loop, but I
want my simple, concise, list comprehension ! And I couldn't find a simple
way to express it.
The pattern can be generically resumed like this :
[transform(e) for e in seq if some_test(transform(e))]
So what about using the 'as' keyword to extend lists comprehensions, and
to avoid calling transform() twice ?
Could be:
[transform(e) as transformed for e in seq if some_test(transformed)]
In my use case I would simply have to write;:
[l.strip() as stripped for l in text.split('\n') if stripped != '']
Which seems to me clear and concise.
Regards,
Tarek
--
Tarek Ziadé | Association AfPy | www.afpy.org
Blog FR | http://programmation-python.org
Blog EN | http://tarekziade.wordpress.com/
[View Less]