Noob question


(IVELIN PEYCHEV) #1

Where does this syntax originate from?

image

Comparing None (or NULL) to object instead of comparing the object with None?

It annoys me, I’d love to know where does it originate from.

Thanks in advance.


#2

That is indeed a bit weird (both the order and needless complexity, unless I’m missing context), the Pythonic approach would be simply:

if obj:

Where’d you copy it from?


(David Eränen) #3

I’m pretty sure it comes from the fact that some people prefer doing if None == obj in order to guard against a typo: if None = obj. The typo would be a compile error because you can’t assign to None, which is good because it would be noticed immediately. If you do if obj = None then at least on C++ it compiles and the bug might go unnoticed.

Btw, I don’t like if None == obj either. Doesn’t read well and my brain gets confused. :slight_smile:

-David


(IVELIN PEYCHEV) #4

But I’ve seen it in other languages too.


#5

I think @dale sometimes writes that way.

And this is a good reason to do it that way, but I also don’t like it :wink:

my exceptions fail in some cases:

    val = 0.0
    if val:
        print val
    else:
        print "val=None?"

(IVELIN PEYCHEV) #6

It is not specifically about someone who wrote this, I’ve seen in many languages. I just want to know if this is based on some history.

Is it a common syntax of CPP or PASCAL or whatever?

My brain rejects this syntax to compare nothing with an object.
It accepts comparing an object to nothing


#7

Yep, if you’re returning numbers and 0 is an acceptable answer, then that will go haywire… I prefer

if obj is None:
    #do something

Which will only branch if the return is truly None - not if it is 0 or False…


#8

If not sure fail is that right word here. This is how Python works, but I can certainly see how it might lead to confusion, if one is not aware (as pointed out by Mitch).


(Nathan 'jesterKing' Letwory) #9

The right word is probably: unexpected.


(IVELIN PEYCHEV) #10

Another thing in that script that caused an error:

image

str is used as an object name and below when I tried to create a string out of the value of indent I got an error:

image

Also when formatting strings I use "".format() instead of string_object.format("",)
But that I think is personal preference. :wink:


#11

str() is one of the built-in Python functions, and should not be used as a variable name (as this will override the function, another perhaps unexpected behaviour).


#12

In C it’s not entirely uncommon to see a null test like “if (NULL == obj)” being written in the following way:

if (obj)
{
   // Not NULL, go ahead 'n do your thing
}

This has to do with the fact that a memory address of NULL is equivalent to 0 (zero), which happens to also be the equivalent to “false” (and any non-zero value would mean “true”). Thus:

if (!obj)
{
   // Ouch! Obj was null (same as obj == 0, or obj == NULL)
}

But sometimes you will see even weirder things than that. What about this one:

char c, *p = "Hello!";
while (c=*p++)
{
    // Here you actually have three (3) things going on inside the 
    // parenthesis while traversing the char array "Hello!":
    // 
    // #1. Copying the current character, which the pointer *p currently 
    //     points at, to the char c (*p starts at 'H').
    // 
    // #2. Determining whether the current char, which the pointer *p
    //     is pointing at, is equal to \0 (the end marker of the string).
    // 
    // #3. if the while-expression didn't terminate (due to 0=null which
    //     is also being interpreted as "false") then the pointer *p (which 
    //     is also actually an integer pointing at a memory address) is 
    //     increased with 1.
    // 
    // #4. In this order (precedence) 
}

Cool. I bet Mr @ivelin.peychev would just love to play around with code like this. :slight_smile:

// Rolf


#13

Also

while(*c++=*p++);

To copy a string’s content. :slight_smile:


#14

That seems like a really nasty one :slight_smile:

// Rolf


(IVELIN PEYCHEV) #15

This is insane. Noob abuser. :rofl:


#16

Arghhh, fail! I was expecting to see you :sob:

:wink:

// Rolf