GithubHelp home page GithubHelp logo

Comments (3)

dwt avatar dwt commented on August 21, 2024

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.

laundmo avatar laundmo commented on August 21, 2024

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.

dwt avatar dwt commented on August 21, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.