Can a lambda function have a bool value of false?Calling a function of a module by using its name (a string)Replacements for switch statement in Python?Hidden features of PythonWhat does the “yield” keyword do?How do I check if a string is a number (float)?How do I return multiple values from a function?Convert bytes to a string?Why are Python lambdas useful?“Least Astonishment” and the Mutable Default ArgumentHow to iterate over rows in a DataFrame in Pandas?

Creating a Master Image to roll out to 30 new Machines Licensing Issues

Are scroll bars dead in 2019?

Do any aircraft carry boats?

Dividing Divisive Divisors

How is the Team Scooby Doo funded?

Is the definition of integral extension, why we use monic polynomial?

Renewed US passport, did not receive expired US passport

Are there take-over requests from autopilots?

The Royal Mint of Alphagonia

Which currencies does Wizz Air use in-flight?

A Little Riddle

Are Democrats more likely to believe Astrology is a science?

SCOTUS - Can Congress overrule Marbury v. Madison by statute?

How do I politely hint customers to leave my store, without pretending to need leave store myself?

I changed a word from a source, how do I cite it correctly?

Are there any instances of members of different Hogwarts houses coupling up and marrying each other?

Do Milankovitch Cycles fully explain climate change?

How can I protect myself in case of a human attack like the murders of the hikers Jespersen and Ueland in Morocco?

My research paper filed as a patent in China by my Chinese supervisor without me as inventor

Is BitLocker useful in the case of stolen laptop?

Kingdom Map and Travel Pace

Can I toggle Do Not Disturb on/off on my Mac as easily as I can on my iPhone?

A medieval fantasy adventurer lights a torch in a 100% pure oxygen room. What happens?

Random point on a sphere



Can a lambda function have a bool value of false?


Calling a function of a module by using its name (a string)Replacements for switch statement in Python?Hidden features of PythonWhat does the “yield” keyword do?How do I check if a string is a number (float)?How do I return multiple values from a function?Convert bytes to a string?Why are Python lambdas useful?“Least Astonishment” and the Mutable Default ArgumentHow to iterate over rows in a DataFrame in Pandas?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















Is it possible, that a lambda function can return a False value, when given to the bool function?



This lambda function for example yields True:



bool(lambda x:[])
True









share|improve this question


























  • Lambdas are just an alternative way to write functions. You can create a class that behaves as a function and is itself falsey, but it would not be a lambda function.

    – khelwood
    Mar 28 at 8:57












  • So functions are always evaluated as true?

    – David
    Mar 28 at 9:01











  • Functions are truthy by default, like nearly everything. You can create something customised, but it wouldn't be a lambda function.

    – khelwood
    Mar 28 at 9:04

















1















Is it possible, that a lambda function can return a False value, when given to the bool function?



This lambda function for example yields True:



bool(lambda x:[])
True









share|improve this question


























  • Lambdas are just an alternative way to write functions. You can create a class that behaves as a function and is itself falsey, but it would not be a lambda function.

    – khelwood
    Mar 28 at 8:57












  • So functions are always evaluated as true?

    – David
    Mar 28 at 9:01











  • Functions are truthy by default, like nearly everything. You can create something customised, but it wouldn't be a lambda function.

    – khelwood
    Mar 28 at 9:04













1












1








1








Is it possible, that a lambda function can return a False value, when given to the bool function?



This lambda function for example yields True:



bool(lambda x:[])
True









share|improve this question
















Is it possible, that a lambda function can return a False value, when given to the bool function?



This lambda function for example yields True:



bool(lambda x:[])
True






python python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 28 at 10:11









cs95

166k32 gold badges240 silver badges303 bronze badges




166k32 gold badges240 silver badges303 bronze badges










asked Mar 28 at 8:53









DavidDavid

5463 silver badges12 bronze badges




5463 silver badges12 bronze badges















  • Lambdas are just an alternative way to write functions. You can create a class that behaves as a function and is itself falsey, but it would not be a lambda function.

    – khelwood
    Mar 28 at 8:57












  • So functions are always evaluated as true?

    – David
    Mar 28 at 9:01











  • Functions are truthy by default, like nearly everything. You can create something customised, but it wouldn't be a lambda function.

    – khelwood
    Mar 28 at 9:04

















  • Lambdas are just an alternative way to write functions. You can create a class that behaves as a function and is itself falsey, but it would not be a lambda function.

    – khelwood
    Mar 28 at 8:57












  • So functions are always evaluated as true?

    – David
    Mar 28 at 9:01











  • Functions are truthy by default, like nearly everything. You can create something customised, but it wouldn't be a lambda function.

    – khelwood
    Mar 28 at 9:04
















Lambdas are just an alternative way to write functions. You can create a class that behaves as a function and is itself falsey, but it would not be a lambda function.

– khelwood
Mar 28 at 8:57






Lambdas are just an alternative way to write functions. You can create a class that behaves as a function and is itself falsey, but it would not be a lambda function.

– khelwood
Mar 28 at 8:57














So functions are always evaluated as true?

– David
Mar 28 at 9:01





So functions are always evaluated as true?

– David
Mar 28 at 9:01













Functions are truthy by default, like nearly everything. You can create something customised, but it wouldn't be a lambda function.

– khelwood
Mar 28 at 9:04





Functions are truthy by default, like nearly everything. You can create something customised, but it wouldn't be a lambda function.

– khelwood
Mar 28 at 9:04












3 Answers
3






active

oldest

votes


















1
















Only the objects specified in Truth Value Testing in the docs, as well as objects whose __bool__ or __len__ methods return False or 0 respectively, are falsy objects.



Everything else (yes, this includes any lambda) is truthy.






share|improve this answer


































    3
















    no, you can not do that with pure lambda expressions.



    lambda x:[]


    is of the type



    <class 'function'>


    and as the documentation says, there is nothing of that type that will turn out to be falsy - so it will come out truthy; i.e. passing it to bool will return True.



    if you want a funtion (a callable) that evaluates to False i would exactly do what is described in khelwood's answer.






    share|improve this answer


































      2
















      Lambdas are just an alternative way to write functions. Like nearly everything, they are truthy by default. You can create a class whose instances behave as functions and are themselves falsey. They wouldn't be lambda functions, but there's no reason that should be a problem.



      For instance:



      class FalseyFunction:
      def __init__(self, func):
      self.func = func
      def __call__(self, *args, **kwargs):
      return self.func(*args, **kwargs)
      def __bool__(self):
      return False

      >>> f = FalseyFunction(lambda x:[])
      >>> f(0)
      []
      >>> bool(f)
      False





      share|improve this answer



























        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );














        draft saved

        draft discarded
















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55393472%2fcan-a-lambda-function-have-a-bool-value-of-false%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1
















        Only the objects specified in Truth Value Testing in the docs, as well as objects whose __bool__ or __len__ methods return False or 0 respectively, are falsy objects.



        Everything else (yes, this includes any lambda) is truthy.






        share|improve this answer































          1
















          Only the objects specified in Truth Value Testing in the docs, as well as objects whose __bool__ or __len__ methods return False or 0 respectively, are falsy objects.



          Everything else (yes, this includes any lambda) is truthy.






          share|improve this answer





























            1














            1










            1









            Only the objects specified in Truth Value Testing in the docs, as well as objects whose __bool__ or __len__ methods return False or 0 respectively, are falsy objects.



            Everything else (yes, this includes any lambda) is truthy.






            share|improve this answer















            Only the objects specified in Truth Value Testing in the docs, as well as objects whose __bool__ or __len__ methods return False or 0 respectively, are falsy objects.



            Everything else (yes, this includes any lambda) is truthy.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jun 19 at 6:54

























            answered Mar 28 at 9:02









            cs95cs95

            166k32 gold badges240 silver badges303 bronze badges




            166k32 gold badges240 silver badges303 bronze badges


























                3
















                no, you can not do that with pure lambda expressions.



                lambda x:[]


                is of the type



                <class 'function'>


                and as the documentation says, there is nothing of that type that will turn out to be falsy - so it will come out truthy; i.e. passing it to bool will return True.



                if you want a funtion (a callable) that evaluates to False i would exactly do what is described in khelwood's answer.






                share|improve this answer































                  3
















                  no, you can not do that with pure lambda expressions.



                  lambda x:[]


                  is of the type



                  <class 'function'>


                  and as the documentation says, there is nothing of that type that will turn out to be falsy - so it will come out truthy; i.e. passing it to bool will return True.



                  if you want a funtion (a callable) that evaluates to False i would exactly do what is described in khelwood's answer.






                  share|improve this answer





























                    3














                    3










                    3









                    no, you can not do that with pure lambda expressions.



                    lambda x:[]


                    is of the type



                    <class 'function'>


                    and as the documentation says, there is nothing of that type that will turn out to be falsy - so it will come out truthy; i.e. passing it to bool will return True.



                    if you want a funtion (a callable) that evaluates to False i would exactly do what is described in khelwood's answer.






                    share|improve this answer















                    no, you can not do that with pure lambda expressions.



                    lambda x:[]


                    is of the type



                    <class 'function'>


                    and as the documentation says, there is nothing of that type that will turn out to be falsy - so it will come out truthy; i.e. passing it to bool will return True.



                    if you want a funtion (a callable) that evaluates to False i would exactly do what is described in khelwood's answer.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Mar 28 at 9:20

























                    answered Mar 28 at 8:55









                    hiro protagonisthiro protagonist

                    27k8 gold badges51 silver badges74 bronze badges




                    27k8 gold badges51 silver badges74 bronze badges
























                        2
















                        Lambdas are just an alternative way to write functions. Like nearly everything, they are truthy by default. You can create a class whose instances behave as functions and are themselves falsey. They wouldn't be lambda functions, but there's no reason that should be a problem.



                        For instance:



                        class FalseyFunction:
                        def __init__(self, func):
                        self.func = func
                        def __call__(self, *args, **kwargs):
                        return self.func(*args, **kwargs)
                        def __bool__(self):
                        return False

                        >>> f = FalseyFunction(lambda x:[])
                        >>> f(0)
                        []
                        >>> bool(f)
                        False





                        share|improve this answer





























                          2
















                          Lambdas are just an alternative way to write functions. Like nearly everything, they are truthy by default. You can create a class whose instances behave as functions and are themselves falsey. They wouldn't be lambda functions, but there's no reason that should be a problem.



                          For instance:



                          class FalseyFunction:
                          def __init__(self, func):
                          self.func = func
                          def __call__(self, *args, **kwargs):
                          return self.func(*args, **kwargs)
                          def __bool__(self):
                          return False

                          >>> f = FalseyFunction(lambda x:[])
                          >>> f(0)
                          []
                          >>> bool(f)
                          False





                          share|improve this answer



























                            2














                            2










                            2









                            Lambdas are just an alternative way to write functions. Like nearly everything, they are truthy by default. You can create a class whose instances behave as functions and are themselves falsey. They wouldn't be lambda functions, but there's no reason that should be a problem.



                            For instance:



                            class FalseyFunction:
                            def __init__(self, func):
                            self.func = func
                            def __call__(self, *args, **kwargs):
                            return self.func(*args, **kwargs)
                            def __bool__(self):
                            return False

                            >>> f = FalseyFunction(lambda x:[])
                            >>> f(0)
                            []
                            >>> bool(f)
                            False





                            share|improve this answer













                            Lambdas are just an alternative way to write functions. Like nearly everything, they are truthy by default. You can create a class whose instances behave as functions and are themselves falsey. They wouldn't be lambda functions, but there's no reason that should be a problem.



                            For instance:



                            class FalseyFunction:
                            def __init__(self, func):
                            self.func = func
                            def __call__(self, *args, **kwargs):
                            return self.func(*args, **kwargs)
                            def __bool__(self):
                            return False

                            >>> f = FalseyFunction(lambda x:[])
                            >>> f(0)
                            []
                            >>> bool(f)
                            False






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 28 at 9:03









                            khelwoodkhelwood

                            34.9k7 gold badges45 silver badges68 bronze badges




                            34.9k7 gold badges45 silver badges68 bronze badges































                                draft saved

                                draft discarded















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55393472%2fcan-a-lambda-function-have-a-bool-value-of-false%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                                용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

                                155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해