Encoding of Python 2 string literals

Chris Angelico rosuav at gmail.com
Wed Jul 22 14:39:56 CEST 2015

On Wed, Jul 22, 2015 at 8:17 PM, anatoly techtonik <techtonik at gmail.com> wrote:
> Is there a way to know encoding of string (bytes) literal
> defined in source file? For example, given that source:
>     # -*- coding: utf-8 -*-
>     from library import Entry
>     Entry("текст")
> Is there any way for Entry() constructor to know that
> string "текст" passed into it is the utf-8 string?

I don't think so. However, if you declare that to be a Unicode string,
the parser will decode it using the declared encoding, and it'll be a
five-character string. At that point, it doesn't matter what your
source encoding was, because the characters entered will match the
characters seen.



More information about the Python-list mailing list