S
That is the impression I got from this statement:If you need three (or four, or fifty)
distinguishable states, then obviously boolean context will not solve
your problem. I never said it would.
But I guess you misunderstood (or were just picking at) the example.How you interpret some_variable = None depends on what some_variable
represents. If some_variable represents "number of jelly beans in a jar",
then that should be 0 if there is no jar.
--> import datetime
--> mn = datetime.time(0)
--> mn
datetime.time(0, 0)
--> mn == 0
False
Apparently, midnight does not equal zero.
Your entire reply is predicated on this idea that I was talking about
writing classes with this extra "isempty" method.
No. I was talking about having "isempty" be part of the collection
interface, and eliminating polymorphic bool conversion.
To put it in duck-typing terms, why should everything have to quack like
True or False? Sure, I can see why 1 quacks like True or [] quacks like
False, but I don't see why say, a Logger or function should quack like
either.
Should a Thread object be True if it's been started and False
otherwise?
If it truly is about something vs. nothing, why is a NameError (or
AttributeError) raised when testing with an undefined variable? Being
undefined quacks like nothing, doesn't it?
This has probably been discussed before, but why is there an implicit
conversion to a boolean in if and while statements?
if not None:
print('hi')
prints 'hi' since bool(None) is False.
If this was discussed in a PEP, I would like a link to it. There are so
many PEPs, and I wouldn't know which ones to look through.
Converting 0 and 1 to False and True seems reasonable, but I don't see
the point in converting other arbitrary values.
I am aware of the default behavior, but the reason for it still eludes me.The default behaviour is that every object is something, hence true-like,
unless explicitly coded to be treated as false-like. Since both loggers
and functions are objects, they are true-like unless the default is
overridden.
I don't want that, but I am suggesting that it would be consistent withAre you suggesting that if x doesn't exist, you want this behaviour?
I am aware of the default behavior, but the reason for it still eludes me.
It already is part of the collection interface: it is spelled __nonzero__
(Python 2) or __bool__ (Python 3), and like all dunder methods, it is
called automatically for you when you use the right syntax:
Andrew said:To put it in duck-typing terms, why should everything have to quack like
True or False? Sure, I can see why 1 quacks like True or [] quacks like
False, but I don't see why say, a Logger or function should quack like
either. Should a Thread object be True if it's been started and False
otherwise?
If it truly is about something vs. nothing, why is a NameError (or
AttributeError) raised when testing with an undefined variable? Being
undefined quacks like nothing, doesn't it?
Don't confuse names and objects. You can only test the truth value ofI don't want that, but I am suggesting that it would be consistent with
the idea of "something or nothing".
Actually, there is no "it". So we cannot talk about how it quacks. :-DNot really. It doesn't quack like anything.
I am aware of the default behavior, but the reason for it still eludes me.
If it truly is about something vs. nothing, why is a NameError (or
AttributeError) raised when testing with an undefined variable?
I am aware of the default behavior, but the reason for it still eludes me..
[...]
If I insist on making a single object do duty for both the jar and the
jellybean count, then I need a "null jar object", and I probably end up
with something like this:
Jar(number_of_beans=None) => null jar object with jar.jellybeans = 0
Jar(number_of_beans=0) => normal jar object with jar.jellybeans = 0
Jar(number_of_beans=42) => normal jar object with jar.jellybeans = 42
and then my code becomes even more complicated and less understandable,
but hey, it's *my* code and I can do whatever damn-fool thing I like!
If you know some English, its clear that if and while
create bool contexts.
[If you know English but have not
studied logic the 'if/while' make sense whereas 'bool' is
gobbledygook]
And which Univeristy would you recommend for studying the intricacies of "gobbledygook"? ;-)
Your entire reply is predicated on this idea that I was talking about
writing classes with this extra "isempty" method.
No. I was talking about having "isempty" be part of the collection
interface, and eliminating polymorphic bool conversion.
Your entire reply is predicated on this idea that I was talking about
writing classes with this extra "isempty" method.
No. I was talking about having "isempty" be part of the collection
interface, and eliminating polymorphic bool conversion.
Because it makes it simple to distinguish between having an object and
not having one without having to explicitly test for it each time.
db = connect("my:db") # or None if the connection failed
if db:
<do something>
I find that usage to be incredibly intuitive.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.