Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

print "Hi" in python 3 exception handling doesn't work #70005

Closed
MrPotato1029 mannequin opened this issue Dec 7, 2015 · 4 comments
Closed

print "Hi" in python 3 exception handling doesn't work #70005

MrPotato1029 mannequin opened this issue Dec 7, 2015 · 4 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@MrPotato1029
Copy link
Mannequin

MrPotato1029 mannequin commented Dec 7, 2015

BPO 25819
Nosy @bitdancer, @zware, @Vgr255

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2015-12-07.22:53:50.182>
created_at = <Date 2015-12-07.22:44:49.534>
labels = ['type-bug', 'invalid']
title = 'print "Hi" in python 3 exception handling doesn\'t work'
updated_at = <Date 2015-12-08.02:46:51.527>
user = 'https://bugs.python.org/MrPotato1029'

bugs.python.org fields:

activity = <Date 2015-12-08.02:46:51.527>
actor = 'zach.ware'
assignee = 'none'
closed = True
closed_date = <Date 2015-12-07.22:53:50.182>
closer = 'abarry'
components = []
creation = <Date 2015-12-07.22:44:49.534>
creator = 'Mr.Potato1029'
dependencies = []
files = []
hgrepos = []
issue_num = 25819
keywords = []
message_count = 4.0
messages = ['256083', '256084', '256091', '256101']
nosy_count = 4.0
nosy_names = ['r.david.murray', 'zach.ware', 'abarry', 'Mr.Potato1029']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue25819'
versions = ['Python 3.5']

@MrPotato1029
Copy link
Mannequin Author

MrPotato1029 mannequin commented Dec 7, 2015

When you execute the code:
try:
print "Hi"
except:
print("Hello")

in python 3.5, it creates a syntax error in Terminal on Mac and a pop-up error in IDLE, while it should just print Hello in the console.

@MrPotato1029 MrPotato1029 mannequin added the type-bug An unexpected behavior, bug, or error label Dec 7, 2015
@Vgr255
Copy link
Mannequin

Vgr255 mannequin commented Dec 7, 2015

The reason you are experiencing this behviour is because of the way Python works. Python needs to compile your code before it can execute it. It parses the code, sees an invalid token ('print "Hi"'), fails to compile and throws an error. Your code never gets executed because Python is not able to compile it in the first place. Try adding a print call before the try-except block and you'll see it never gets executed either :)

@Vgr255 Vgr255 mannequin closed this as completed Dec 7, 2015
@Vgr255 Vgr255 mannequin added the invalid label Dec 7, 2015
@bitdancer
Copy link
Member

By the way, if your goal is to write python2/3 compatible code, notice that 'print("hello")' is valid in python2 and will do the same thing as print "hello"...as long as you don't use commas in the argument list to print.

@zware
Copy link
Member

zware commented Dec 8, 2015

Also, if you only need to support Python 2.6+, you can use 'from __future__ import print_function' and get all the benefits of 'print' as a function in Python 2 (except the 'flush' argument, which was added in Python 3.3).

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants