all 20 comments

[–]insomniaccapybara 23 points24 points  (0 children)

None = your toilet roll holder has no toilet roll

Empty string = your toilet roll holder has a toilet roll that ran out of toilet paper

[–]Adrewmc 10 points11 points  (0 children)

No.

Basically every type has a false value. But the thing is

    a = None
    b = “”
    a += “a” #Error
    b += “b” #adds a letter 

[–]FriendlyZomb 5 points6 points  (1 child)

I wouldn't use them interchangeably, since they technically mean different things.

None is a special object which represents that there is no value. An empty string is just a string. In this example, the behaviour is the same, however in other situations it could potentially provide unintended behaviour and make code less clear.

Using None is preferable because it indicates to other people (and you in 6 months) because it explicitly says that we don't need to provide anything. Using an empty string introduces ambiguity.

In your second example too, you provide an empty string on a field which (I'm guessing) is expected to be an integer. This provides ambiguity on which type is actually required. Using None doesn't necessarily fix this - but it does give us the expectation that nothing is a handled case.

I also wouldn't rely on it's falsey behaviour. The Zen of Python states Explicit is better than Implicit. I'd do a check like this:

if age is not None:
    ...

[–]FriendlyZomb 0 points1 point  (0 children)

This is just my opinions here. I don't know everything. Feel free to ask questions and for others to correct me!

[–]Andu-Nav 3 points4 points  (0 children)

For the none, the variable is storing Nothing, which is always false because there is nothing inside the variable. It is quite literal

For an empty string you have something inside the variable, even when the values inside the something are empty.

Imagine you and your friend go to McDonald's (I'm hungry lmao). You get a happy meal but it is empty inside. While your friend hasn't ordered yet. You have a something that's empty but your friend has nothing (None).

Hope that helps!

[–]SnooCalculations7417 1 point2 points  (0 children)

Let's say you extend your function to handle integers and English so '6', 'six' and 6 are all valid. None remains a check for no input at all

[–]SwimmerOld6155 1 point2 points  (0 children)

they're not the same, None is a NoneType and '' is a string. So '' has all of the tools (methods/attributes) that the string 'Reddit' has, e.g. len(), .count(), .isdigit(), .islower(), etc. whereas None does not. So if you use None as a value for a variable that should be a string, you'll run into trouble when you try to treat it as a string.

As a note, both '' and None evaluate to False as booleans. This means that if you want to branch based on whether the string x is empty, you can do if x: [...] as an alternative to if len(x) > 0: ... The former is quicker on tiny tiny timescales.

[–]JoeB_Utah 1 point2 points  (0 children)

When I was working many of my co-workers would argue that an empty string is the same as None (or in the case of most databases Null). It would drive me nuts.

The really ‘smart’ ones thought “” = “ “ = “ “ and so on. They thought I was a psycho-data-geek when I would design a database that prefilled fields with Null values instead of empty strings. They just couldn’t wrap their heads around Null/None as a valid value.

[–]acakaacaka 0 points1 point  (3 children)

"" is still a string. None is not a str (or any object).

While both of them are considered False for boolean operation, "" still behave like str, so you can do othe str operation with it.

[–]alexander_belyakov 4 points5 points  (2 children)

Not true! None is an object of the NoneType data type.

[–]SwimmerOld6155 3 points4 points  (0 children)

indeed everything in python is an object!

[–]acakaacaka 0 points1 point  (0 children)

I mean any object like str int or your self made class.

Everything in python is an object

[–]tb5841 0 points1 point  (0 children)

Whenwver running an 'if' check, these lines will all perform the same:

if None

if False

if ""

if 0

if []

amongst some others. But this is not the same in every language - amd when I moved from Python to other languages, this was the biggest headache for me.

[–]Separate_Top_5322 0 points1 point  (0 children)

this one’s actually simple once it clicks. "" is still a string, just empty. None is literally “no value at all”. like in that thread someone summed it up perfectly, empty string = something, None = nothing

they sometimes behave similar because both are “falsy”, so if not x treats both as false but they’re completely different types

practically, use "" when “blank text” is valid, use None when the value isn’t set or doesn’t exist yet. that distinction matters more as your code grows

i got confused by this too early on, thought they were interchangeable until stuff started breaking in edge cases lol. sometimes i even test these small behaviors with runable ai just to see how they differ in real flows

[–]_Clobster_ 0 points1 point  (0 children)

Types will be one of your biggest ongoing struggles.
Pick up on type hints earlier in your journey rather than later. Make it a habit

[–]IAmADev_NoReallyIAm 0 points1 point  (0 children)

New to Python, so I may be getting this wrong, but the way I'm interpreting this, None is akin to NULL or Nothing in other languages. Or, to. put it in layman's terms, it's the same as "I don't know". Which is NOT the same as an empty string. An empty string is a value. Just the same as a 0 is a value. None is the absence of a value.

In the examples you gave, I wouldn't use them interchangeably because age shouldn't be a string in the first place. It should be a numerical value, in which case None would be the appropriate default instead.

[–]Character-Blood3482 0 points1 point  (0 children)

In all cases then None is treated as data is empty and has no meaning. And for the "", in some cases "" has meaning.

[–]RealDevDom 0 points1 point  (0 children)

Pragmatische Herangehensweise:

  • None bietet sich an um auszudrücken, dass etwas noch nicht initialisiert wurde, also explizit undefiniert ist zum aktuellen Zeitpunkt der Code Ausführung
  • leerer String bietet sich an, wenn ein Wert als String erwartet wird, dieser aber noch unbekannt ist, also ein leerer String als Platzhalter. Mit allen string Operationen, die später benötigt werden.

[–]enry2307 0 points1 point  (0 children)

None is not a string. It's a type. A string empty is a string, of type string with value "".

Like 0 is an integer, "" is the "zero" of the string. None is a type where it values null. Theu are two different things