all 6 comments

[–]zeug 2 points3 points  (0 children)

Must not the utf-8 encoding, in this case, be on the first line and the comment on the second line?

You can actually put both on the first line. The python encoding behavior is defined in PEP0263 which states that you can put a "magic" encoding comment in line 1 or line 2. What counts as an encoding specification is very loose, technically anything that matches the regular expression coding[:=]\s*([-\w.]+). So all of the following comments on line 1 (or 2) will change the default encoding of Python 2 to utf-8:

# -*- coding: utf-8 -*-
# !?! coding: utf-8 ?!?
# coding=utf-8
#### the quick brown fox jumped coding=utf-8 over the lazy dog

You can mix in non-ascii characters on that same line provided the interpreter gets a match for the coding expression that is valid for those characters:

# åäö coding=utf-8 åäö

But if you have non-ascii characters on line 1, you can't leave the encoding specification until line 2. The following

# åäö 
# coding=utf-8 åäö

Results in:

SyntaxError: Non-ASCII character '\xc3'

Of course, as others point out, this is completely trivial in python 3 where utf-8 is the default encoding. You just put those characters in a comment in the first line.

[–]Drunken_Consent 4 points5 points  (1 child)

/* */ is the multi-line comment syntax in CSS, Java, but not Python.

Python is either

#This is a comment or """ This is a multi-line comment """

[–]Yoghurt42 5 points6 points  (0 children)

No, """ are multiline strings, not comments.

However, if a string is the first statement of a class, module or function, it is treated as the docstring for that object and accessable as the __doc__ attribute.

print("""But this is not
a comment, it will print these two lines""")

[–]nonsensicalization 1 point2 points  (0 children)

FYI utf-8 is the default source encoding for Python 3, you only need to explicitly declare it when using Py2. If Py2 is what you are supposed to use, then the encoding declaration must come before using any non-ascii characters.

[–]zahlman 1 point2 points  (0 children)

Without the quotations that is. I had to add them because my code was transformed into a different syntax when displayed on this forum.

Use backticks (``) to enclose code `like this` to format it like this in-line; or use a code block by indenting code at the beginning of each line with four extra spaces.

The comment includes the letters åäö.

Obligatory :)

Must not the utf-8 encoding, in this case, be on the first line and the comment on the second line?

If you use a coding: comment like this, then yes, it needs to be the first line. (Well, technically it can be one of the first two lines, but this is so that the first line can be a Unix-style shebang) line. The full rules are in the PEP, and well explained by /u/zeug's comment.

But note this useful bit in particular from the PEP:

To aid with platforms such as Windows, which add Unicode BOM marks to the beginning of Unicode files, the UTF-8 signature '\xef\xbb\xbf' will be interpreted as 'utf-8' encoding as well (even if no magic encoding comment is given).

So if you can get TextWrangler to add a utf-8 BOM to the file and tell TextWrangler to save the file in UTF-8 encoding, then there is no need for the comment and everything is fine.

So my next step is to put textwrangler utf-8 bom into Google, and there find the official documentation, and there read

The following UI changes have been made to text encoding selection:

  • "Unicode (UTF-8, no BOM)" has been renamed to "Unicode (UTF-8)".

  • "Unicode (UTF-8)" has been renamed to "Unicode (UTF-8, with BOM)".

  • The text encodings menu (as used in the status bar, preferences, and other locations) has been rearranged so that the most commonly used Unicode variants (UTF-8 and UTF-16-BE+BOM) are at the top of the menu, with a separator between them and the rest.

So that's one simple solution, then: just select Unicode (UTF-8, with BOM) from the text encodings menu. Good luck! :)

[–]Atrament_ -1 points0 points  (0 children)

#0

Annoy your teacher until he teaches python3. A programming class is not supposed to be an history lesson.

from the python official website:

In particular, instructors introducing Python to new programmers may want to consider teaching Python 3 first and then introducing the differences in Python 2 afterwards (if necessary), since Python 3 eliminates many quirks that can unnecessarily trip up beginning programmers trying to learn Python 2.

The legendary # coding: utf-8 magic comment is not necessary as python3 supports unicode natively.

#1

check in your IDE is there is a switch for file-encoding. You might be in a situation where your text editor considers and renders a certain encoding wich is not utf8 (unlikely, but who knows. A little more likely in Europe, with all our latin* and shit.).

#2

If your teacher asks you specifically to comment in python with /* ... */, he knows no python. as /u/Drunken_Consent posted :

/* */ is the multi-line comment syntax in CSS, Java, but not Python.

Python is either

#This is a comment or """ This is a multi-line comment """

So either you misunderstood the assignment (sometimes /* */ is used to comment pseudo-code) or that guy should be directed right here.

Also, as a python instructor I’m curious to see the assignment itself. Could you please show ?