mercredi 28 mars 2018

Fluent interface coding style with standard Python 3 functions? [duplicate]

This question already has an answer here:

I came to Python from Ruby, where OO is baked deeply into the language and coding in a fluent-interface style comes very naturally. Things that seem like they should work in Python 3 just don't. For example, I can fire up iPython and do:

In [21]: foo = list(range(1,11))

In [22]: type(foo)
Out[22]: list

In [23]: foo
Out[23]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

In [24]: foo.append(11)

In [25]: foo
Out[25]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

But trying to condense this doesn't work:

In [26]: foo = list(range(1,11)).append(11)

In [27]: foo

In [28]: type(foo)
Out[28]: NoneType

Am I missing something, or does OO in Python just not run that deep? I also notice that writing len(foo) is preferred over foo.len() which doesn't exist and has to be written foo.__len__(), which is ugly enough that nobody is going to write it.

Aucun commentaire:

Enregistrer un commentaire