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;
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
add a comment |
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
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
add a comment |
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
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
python python-3.x
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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.
add a comment |
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.
add a comment |
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
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
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
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
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Mar 28 at 9:03
khelwoodkhelwood
34.9k7 gold badges45 silver badges68 bronze badges
34.9k7 gold badges45 silver badges68 bronze badges
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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