LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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-01-2018, 02:44 PM   #1
vysero
Member
 
Registered: May 2018
Posts: 137

Rep: Reputation: Disabled
return statements and the differences between python 2 and 3


I recently stumbled on the following return statement written in Python 2:

Code:
def __str__(self):
    return `self.value`
Tossing this into my PyDev 3.5.2 compiler I got a lexical error and when I removed the ' ' surrounding the return statement it went away. I never used Python 2 so I am left wondering if the statement is still equivalent and what exactly the purpose of putting ' ' around a return statement was in Python 2.
 
Old 06-01-2018, 03:34 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,219

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
Quote:
what exactly the purpose of putting ' ' around a return statement
The quotes or backtics were obviously a mistake and weren't meant to be there.
 
Old 06-01-2018, 03:37 PM   #3
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
The return statement is a red herring.

Backticks were deprecated and replaced by the repr() function. So, you actually changed the meaning of that function by removing the backticks and not replacing them with anything.

https://stackoverflow.com/questions/...nterpreter-num

etc.

Last edited by hydrurga; 06-01-2018 at 03:39 PM.
 
1 members found this post helpful.
Old 06-01-2018, 03:44 PM   #4
vysero
Member
 
Registered: May 2018
Posts: 137

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by hydrurga View Post
The return statement is a red herring.

Backticks were deprecated and replaced by the repr() function. So, you actually changed the meaning of that function by removing the backticks and not replacing them with anything.

https://stackoverflow.com/questions/...nterpreter-num

etc.
Ah I see okay good to know!
 
Old 06-01-2018, 03:54 PM   #5
vysero
Member
 
Registered: May 2018
Posts: 137

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by hydrurga View Post
The return statement is a red herring.

Backticks were deprecated and replaced by the repr() function. So, you actually changed the meaning of that function by removing the backticks and not replacing them with anything.

https://stackoverflow.com/questions/...nterpreter-num

etc.
Maybe I could pick your brain a bit more hydrurga. I believe I ran into another red herring. Did the call: log and sys have a special meaning that changed from python 2 to python 3? For instance, the code throwing an error is:

Code:
log(traceback.extract_tb(sys.exc_traceback))
and the compiler is saying: Undefined variable: log and Undefined variable: sys
 
Old 06-01-2018, 04:10 PM   #6
vysero
Member
 
Registered: May 2018
Posts: 137

Original Poster
Rep: Reputation: Disabled
Okay so sys.exc_traceback was definitely removed. Still not sure about log(). Also, not sure what I should be changing sys.exc_traceback too. I found documentation about how its been changed but not what its been replaced by lol

Last edited by vysero; 06-01-2018 at 04:20 PM.
 
Old 06-01-2018, 04:28 PM   #7
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
I have an earth-shattering confession to make, vysero. I hadn't even heard of backticks in Python 2 before you posted your thread, but I decided to help you by searching on the web for "Python 2" "Python 3" backticks and came up with the info. So, my brain isn't really worth picking.

A search on the internet shows that sys.exc_traceback is now deprecated and replaced by exc_info() - https://docs.python.org/2/library/sys.html

It looks as if the log() function comes with the logging module (https://docs.python.org/2.6/library/...ml#logging.log). You'll probably need to look at how the logging module works in Python 3 to figure out why the function isn't recognised. The module has perhaps changed in the way it is loaded or its functions called - https://docs.python.org/3/howto/logging.html

Converting between a Python 2 script and a Python 3 one, without looking into how the script actually works, is a wee bit risky because some changes enforced between the two versions may have subtle effects within the script.
 
Old 06-01-2018, 04:46 PM   #8
vysero
Member
 
Registered: May 2018
Posts: 137

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by hydrurga View Post
I have an earth-shattering confession to make, vysero. I hadn't even heard of backticks in Python 2 before you posted your thread, but I decided to help you by searching on the web for "Python 2" "Python 3" backticks and came up with the info. So, my brain isn't really worth picking.

A search on the internet shows that sys.exc_traceback is now deprecated and replaced by exc_info() - https://docs.python.org/2/library/sys.html

It looks as if the log() function comes with the logging module (https://docs.python.org/2.6/library/...ml#logging.log). You'll probably need to look at how the logging module works in Python 3 to figure out why the function isn't recognised. The module has perhaps changed in the way it is loaded or its functions called - https://docs.python.org/3/howto/logging.html

Converting between a Python 2 script and a Python 3 one, without looking into how the script actually works, is a wee bit risky because some changes enforced between the two versions may have subtle effects within the script.
Still very helpful, thank you. However, for some reason my compiler is having a problem with sys and not with the .exe_traceback (which I changed to .exe_info() btw).
 
Old 06-01-2018, 04:50 PM   #9
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by vysero View Post
Still very helpful, thank you. However, for some reason my compiler is having a problem with sys and not with the .exe_traceback (which I changed to .exe_info() btw).
What problem is it having with sys?
 
Old 06-01-2018, 04:58 PM   #10
vysero
Member
 
Registered: May 2018
Posts: 137

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by hydrurga View Post
What problem is it having with sys?
Okay I think I figured out the problem. I believe, that sys did in fact need to be imported in py2; however, since sys was being imported by all the other scripts that were calling this particular script the fact that it was never imported here made no noticeable difference. So basically, this particular script had an error that was never noticed for over 12 years lol, oh well!
 
  


Reply



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
[SOLVED] If statements in Python QueenSvetlana Programming 8 09-20-2017 02:17 AM
LXer: Python If Statements LXer Syndicated Linux News 0 11-13-2016 04:51 PM
[SOLVED] Python - Issues with if statements and booleans for text adventure project Gary36 Programming 6 09-15-2015 03:58 PM
Python Conditional Statements Based on Success of Last Command metallica1973 Programming 3 10-30-2013 03:46 AM
does two malloc statements return same address? hetzme Programming 5 08-16-2005 10:22 AM

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

All times are GMT -5. The time now is 09:50 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
Open Source Consulting | Domain Registration