LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-14-2018, 11:39 AM   #1
vysero
Member
 
Registered: May 2018
Posts: 32

Rep: Reputation: Disabled
Substitute for types.InstanceType that works in py2 and py3?


Hi there! So I am attempting to update some old legacy code. However, I can't update all the old legacy code because we have unit tests running every night in py2 that is tied in with C code. So I am attempting to write code that will compile in both py2 and py3 and its been tricky. Especially considering I have only been working with python for about 2 weeks.

In any case I have run into a road block this morning with the follow comparison:

Code:
  if ((type(pos1)==types.InstanceType and pos1.__class__==LatLong) or (
    type(pos2)==types.InstanceType and pos2.__class__==LatLong)):
    u1=pos1
because types.InstanceType doesn't exist anymore in py3. I am hoping someone knows of a different way to make this comparison that will compile in py2 and py3?
 
Old 06-14-2018, 11:45 AM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: Slackware
Posts: 8,178

Rep: Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310
Here's how one project handled it:

https://github.com/Nodd/numpy/blob/2.../utils.py#L564
https://github.com/numpy/numpy/pull/3885
https://github.com/numpy/numpy/issues/3897
 
Old 06-14-2018, 12:52 PM   #3
vysero
Member
 
Registered: May 2018
Posts: 32

Original Poster
Rep: Reputation: Disabled
Red face

Here is what I came up with for a different method of doing it, tell me what you think. I dont believe this breaks the logic but it might have. It certainly god rid of the error but that might not be a good thing:

Code:
 if((pos1 is LatLong)or(pos2 is LatLong)):
    u1=pos1
 
Old 06-14-2018, 01:03 PM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: Slackware
Posts: 8,178

Rep: Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310
I would have at least proposed:

Code:
if isinstance(pos1, LatLong)
It's not as rigorous a check as in the original code, but I'd go with it if it works for what the code is doing.

Last edited by dugan; 06-14-2018 at 01:19 PM.
 
Old 06-14-2018, 02:01 PM   #5
vysero
Member
 
Registered: May 2018
Posts: 32

Original Poster
Rep: Reputation: Disabled
How about:

Code:
if((isinstance(pos1,LatLong)and pos1.__class__==LatLong)or(isinstance(pos2,LatLong)and pos2.__class__==LatLong)):
    u1=pos1
Edit: I settled on saying:

Code:
if((type(pos1) is LatLong)or(type(pos2) is LatLong)):
    u1=pos1

Last edited by vysero; 06-14-2018 at 02:45 PM.
 
Old 06-15-2018, 05:56 PM   #6
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 578

Rep: Reputation: 266Reputation: 266Reputation: 266
I don't believe the parentheses are needed in Python. Although, it shouldn't do any harm in this case.

Code:
if((type(pos1) is LatLong)or(type(pos2) is LatLong)):
    u1=pos1
Code:
if type(pos1) is LatLong or type(pos2) is LatLong:
    u1=pos1
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Python: setuptools-0.6c11-py2.6.egg-info install error x201s Linux - Software 5 07-06-2011 06:05 AM
[SOLVED] Conflicting type declarations between sys/types.h and linux/types.h Da Shi Cao Programming 3 09-13-2010 11:55 PM
[SOLVED] Function that only works when casting types? yaplej Programming 8 11-19-2009 05:35 PM
sys/types.h & linux/types.h conflict while compiling johnnyhal Linux - Software 1 12-28-2008 06:39 PM
conflicting redeclaration of sys/types.h and linux/types.h schmil Programming 6 12-11-2008 02:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration