Comments (3)
Well, it's quite some time ago that I really thought about this and decided that this is the best way to go. If I remember correctly, I deduced that this behaviour is less surprising.
Ah, I've documented it in the code. :-)
@property
def self(self):
"""Returns the previous wrapped object. This is especially usefull for APIs that return None.
For example ``_([1,3,2]).sort().self.print()`` will print the sorted list, even though
``sort()`` did return ``None``.
This is simpler than using .previous as there are often multiple wrappers involved where you might expect only one.
E.g. ``_([2,1]).sort().self._ == [1,2]`` but ``_([2,1]).sort().previous._`` will return the function ``list.sort()``
as the attrget and call are two steps of the call chain.
This eases chaining using APIs that where not designed with chaining in mind.
(Inspired by SmallTalk's default behaviour)
"""
# This behavior is always triggered, not just in the `None` return case to avoid
# code that behaves differently for methods that _sometimes_ return `None`.
# Depending on wether the previous method was a transplanted method
# we need to go back one level or two
if isinstance(self.previous, CallableWrapper):
return self.previous.previous
else:
return self.previous
I think that is still quite a valid reason - code that sometimes returns None and sometimes doesn't would trigger really strange bugs otherwise. Or am I missing something?
from fluent.
You are right, i had not considered methods that sometimes return none. It might be possible to use AST to look up whether there is any explicit returns in the function, and only fall back if the only possible return is the implicit None, but that seems like a lot of effort.
from fluent.
Yes. And probably brittle as hell, given that things like decorators will wreak havoc with this. I'll close this for now, if there's anything we missed, feel freer to reopen / add to the conversation.
from fluent.
Related Issues (12)
- Typehinting for IDE suggestions. HOT 3
- How to convert iterable of tuples to dict? HOT 2
- python 3.12 import fails HOT 2
- Thank you! I needed this fluent interface library you can not believe how much. HOT 1
- Question: are aggregations possible - and more generally some example snippets HOT 11
- Possible solution to "call()" HOT 10
- New release? HOT 16
- New Features on _.each HOT 11
- Better subprocess integration HOT 5
- Automatic unwrapping of _.each
- How to type conversion? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fluent.