return statements and the differences between python 2 and 3
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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
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
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.
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.
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.
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).
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).
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!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.