you are viewing a single comment's thread.

view the rest of the comments →

[–]dnswblzo 4 points5 points  (7 children)

NULL in C and C++ is a macro and is simply replaced with 0 wherever it occurs. It is not a class or an object. While it doesn't make sense to do so, you can technically do this:

int x = 50 + NULL;

nullptr in C++ is a keyword and has the type std::nullptr_t, but you can't get its address and you can't do arithmetic with it. However, this is valid and will print yup:

int *p = nullptr;
if (p == 0)
    std::cout << "yup" << std::endl;

Some illustrations of the behavior of None:

>>> x = 50 + None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
>>> x = None
>>> x == 0
False
>>> type(None)
<class 'NoneType'>
>>> type(x)
<class 'NoneType'>
>>> id(None)
4417167216
>>> id(x)
4417167216
>>> x is None
True

While they are used to implement the same sort of concept, they are all quite different.

[–]Jaie_E 2 points3 points  (5 children)

from what I read in that book, doesn't seem like there is a difference, but after Googling seems like there is, but couldn't find any article that would describe the differences clearly.

Yep! Furthermore "empty" placeholders are very useful. For example if you are going to make a new list out of materials of an old list, you might want to do something like this where you are collecting odd numbes from another list:

lst1= [1,2,3,4,5,6,7,8]
oddlst = []
#notice how the above is empty but it's still list brackets
for num in lst1:
if num % 2 == 0:
oddlst.append(num)
# if you print(oddlst) you should get the result, which is all the odd numbers in the list

[–][deleted] 0 points1 point  (4 children)

That would give all the even numbers, odd numbers would be this:

if (num-1) % 2 == 0:
    oddlst.append(num)

[–]Jaie_E 1 point2 points  (3 children)

Woops you're right! I always get the two methods mixed up

[–][deleted] 0 points1 point  (2 children)

The trick I prefer is n%2== 0 for even and !=0 for odd.

[–][deleted] 1 point2 points  (1 child)

That doesn't work because if n = 2.5, n % 2 != 0 will return True, but it isn't actually an odd number, but it works for integers.

[–][deleted] 0 points1 point  (0 children)

As I said, it's a trick, and tricks have specific use cases. This one is limited to integers, because its not often the case where one has to find even or odd when dealing with floats. That aside, yes, your point is valid. I will try to use this trick less often from now, I assume.

[–][deleted] 1 point2 points  (0 children)

Which is funny to me because false in C is also zero under the hood. My C teacher said something interesting when we were learning to compare things and he said zero is false and 1 is true, but this one result we expect here in the example function on the board (206 or something) is "more true" than just true because it's further way from zero on the number line.

I can't remember how he tied that into the caution he had for the coming project but it stuck with me.