Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6859737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:19:14+00:00 2026-05-27T02:19:14+00:00

I learned from the websitethat I should add the code declaration in python when

  • 0

I learned from the websitethat I should add the code declaration in python when i wan’t to input friendly unicode characters: http://www.python.org/dev/peps/pep-0263/, but I still feel confused about it.

Assume that i work in linux with vim, and i create a new py file and input codes as follows:

#!/usr/bin/python2.7
# -*- coding: utf8 -*-
s = u'ޔ'
print s

1. I tried to replace line 2 with codes as follows:

import sys
reload(sys)
sys.setdefaultencoding('utf8')

but it doesn’t work, aren’t they same?

2. I am not very familiar with linux, I really dont know why should i add _*_ at the beginning and end of code delcaration, and when i tried to replaced # -*- coding: utf8 -*- with # code=utf8 or # code: utf8, I got an error:

File "pythontest.py", line 3
SyntaxError: Non-ASCII character '\xde' in file pythontest.py on line 3, but no encoding declared; see     http://www.python.org/peps/pep-0263.html for details

but these code declaration is mentioned in the website http://www.python.org/dev/peps/pep-0263/!

and according to the documentation , the code declaration as follows is allowed:

# This Python file uses the following encoding: utf-8

Oops, what’s this? I don’t think it can be recognized by computer.what in the world should the code declared? I feel more and more confused.

Thanks for help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:19:15+00:00Added an answer on May 27, 2026 at 2:19 am

    The abstract of the PEP you link really says it all:

    This PEP proposes to introduce a syntax to declare the encoding of
    a Python source file
    . The encoding information is then used by the
    Python parser
    to interpret the file using the given encoding. Most
    notably this enhances the interpretation of Unicode literals in
    the source code and makes it possible to write Unicode literals
    using e.g. UTF-8 directly in an Unicode aware editor.

    (the emphasis is mine).

    Even if what you wanted to do would have worked (replacing the encoding of the source file programmatically), it wouldn’t have had any sense. Think about it: the code is static (doesn’t change). It would make no sense to try to read it with different encoding: there is only one correct one (the one the author of the source edited the source in).

    As for the syntax:

    # This Python file uses the following encoding: utf-8
    

    the PEP itself says that that syntax is “Without interpreter line, using plain text”. It is placed there for humans. So that if you open a file in a text editor and find it full of gibberish, you can manually set the encode of the source in its menu.

    EDIT: As for why you should put the encoding between # -*- and -*-… That’s purely conventional. The first symbol, the hash sign, tells that that is a comment (so it won’t be compiled to bytecode), then the _*_ is just a way to tell the parser that that specific comment is for him/her.

    It is not any different than putting in your source:

    # TODO: fix this nasty bug
    

    in which the TODO: part tells the developer (and some IDE) that this is a message requiring an action. You could have really used whatever your wanted, including @MarkZar or WTF!… just convention!

    HTH!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have learned so much from http://www.summerofnhibernate.com/ nhibernate screen casts that i wonder why
Using PDO with prepared statements (I learned from here: http://www.kitebird.com/articles/php-pdo.html ) I'm using a
From reading the Apple Docs on Core Data, I've learned that you should not
I am using Code::blocks to compile my first multiple source file, learned from C
I learned from books that you should write for loop like this: for(var i=0,
So to add onto what I learned from this question I asked: F# Records:
Several months ago I learned from an answer on Stack Overflow how to perform
As I just learned from this question , .NET regexes can access individual matches
I'm a total newbie at Entity Framework and ASP.Net MVC, having learned mostly from
From this question I learned Double.NaN is not equal to itself. I was verifying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.