Python Forum
Question about types and py2 to py3 diffrences
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question about types and py2 to py3 diffrences
#1
I have been reading a bit on some of the py2 to py3 changes. I am a bit confused on some of the changes, for instance if I write in py2:

        if type(speed)==types.NoneType:
            speed = self.data.speed
and then I convert that using py2to3 it gives me:

if type(speed)==type(None):
            speed = self.data.speed
So in py2 the type of None is: <type 'NoneType'> where in py3 the type of None is <class 'NoneType'>. I guess what confuses me (coming from the java world) when I think of objects I think of having access to said objects methods. However, there are no methods for NoneType so what was the point on making it a class?

Also, if speed is defined to be none:

speed = None
Then would:
if type(speed)==type(None):
            speed = self.data.speed
be == to:
        if speed == None:
          speed = self.data.speed
in both py2 and py3?
Reply
#2
(Jun-22-2018, 04:39 PM)Vysero Wrote: I guess what confuses me (coming from the java world) when I think of objects I think of having access to said objects methods. However, there are no methods for NoneType so what was the point on making it a class?
Python doesn't have null like Java does. So None is like Java's null, except that it's an object, since Python doesn't have Java's notion of primitives.

For the other part of your question - yes, you can use == with None but generally is is preferred since None is a singleton object and what you really want is identity, not equality (by default nothing in Python is equal to None, but the speed object could in theory override its == behavior to be equal, while no such flexibility exists for is. Generally, type-checking is discouraged in Python.
Reply
#3
(Jun-22-2018, 05:32 PM)micseydel Wrote:
(Jun-22-2018, 04:39 PM)Vysero Wrote: I guess what confuses me (coming from the java world) when I think of objects I think of having access to said objects methods. However, there are no methods for NoneType so what was the point on making it a class?
Python doesn't have null like Java does. So None is like Java's null, except that it's an object, since Python doesn't have Java's notion of primitives.

For the other part of your question - yes, you can use == with None but generally is is preferred since None is a singleton object and what you really want is identity, not equality (by default nothing in Python is equal to None, but the speed object could in theory override its == behavior to be equal, while no such flexibility exists for is. Generally, type-checking is discouraged in Python.

Ah I see so I should be saying (in both py2 and py3):

if speed is None:
      speed = self.data.speed
You said type checking is discouraged in Python, can you clarify? For instance, with my example how else might I go about setting speed = self.data.speed when speed = None but not when it already has a value? For instance:

    if speed is None:
      speed = self.data.speed
    self.data.speed = speed
instead, I should say?
Reply
#4
Yes, you showed good use of is. When I say type checking is discouraged, I mean using type(x) == blah. Using is with an object (None) is very normal. (When you really need it, isinstance is used instead.)
Reply
#5
You can say:
if speed:
      speed = self.data.speed
None, an empty list, tuple, dict, set, string, etc., zero ( 0 ) and False are evaluated to False.

>>> for element in [None, [], {}, "", tuple(), set(), 0, False]:
...     print(bool(element))
... 
False
False
False
False
False
False
False
False
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
If a zero-speed should be disallowed then that can be useful, but is None is semantically different from checking falsiness. The normal idiom for default values in function definition is to check for None, unless you have a good reason to use falsiness (e.g. in the difference between accepting any empty collection or not).
Reply
#7
(Jun-22-2018, 07:17 PM)micseydel Wrote: Yes, you showed good use of is. When I say type checking is discouraged, I mean using type(x) == blah. Using is with an object (None) is very normal. (When you really need it, isinstance is used instead.)

Okay I think I understand. I sort of understand when I would use isinstance, I think:

    if isinstance(loc,LatLong):
      loc = loc.UTM()
as opposed to:

if type(loc) == types.InstanceType and loc.__class__ == LatLong:
            loc = loc.UTM()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020