Question about types and py2 to py3 diffrences - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Question about types and py2 to py3 diffrences (/thread-11101.html) |
Question about types and py2 to py3 diffrences - Vysero - Jun-22-2018 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.speedand then I convert that using py2to3 it gives me: if type(speed)==type(None): speed = self.data.speedSo 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 = NoneThen would: if type(speed)==type(None): speed = self.data.speedbe == to: if speed == None: speed = self.data.speedin both py2 and py3? RE: Question about types and py2 to py3 diffrences - micseydel - Jun-22-2018 (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.
RE: Question about types and py2 to py3 diffrences - Vysero - Jun-22-2018 (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. Ah I see so I should be saying (in both py2 and py3): if speed is None: speed = self.data.speedYou 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 = speedinstead, I should say? RE: Question about types and py2 to py3 diffrences - micseydel - Jun-22-2018 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.)
RE: Question about types and py2 to py3 diffrences - wavic - Jun-22-2018 You can say: if speed: speed = self.data.speedNone, 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 RE: Question about types and py2 to py3 diffrences - micseydel - Jun-22-2018 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).
RE: Question about types and py2 to py3 diffrences - Vysero - Jun-22-2018 (Jun-22-2018, 07:17 PM)micseydel Wrote: Yes, you showed good use of 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() |