Skip to content

Instantly share code, notes, and snippets.

@mattboehm
Created March 9, 2017 18:32
Show Gist options
  • Save mattboehm/98ae4871ae21821c21cb9f2aecbb9c39 to your computer and use it in GitHub Desktop.
Save mattboehm/98ae4871ae21821c21cb9f2aecbb9c39 to your computer and use it in GitHub Desktop.
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import string
>>> string.letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> help(string)
>>> string.letters
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
@merwok
Copy link

merwok commented Mar 9, 2017

@wimglenn
Copy link

wimglenn commented Mar 9, 2017

Your system locale might be set to ('en_WTF', 'WAT-8').

@moreati
Copy link

moreati commented Mar 9, 2017

Same on Ubuntu 16.10, CPython 2.7

Python 2.7.12+ (default, Sep 17 2016, 12:08:02) 
[GCC 6.2.0 20160914] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import string
>>> string.letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> help(string)

>>> string.letters
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'

but not CPython 3.5

Python 3.5.2+ (default, Sep 22 2016, 12:18:14) 
[GCC 6.2.0 20160927] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import string
>>> string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> help(string)

>>> string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> 

@kms70847
Copy link

kms70847 commented Mar 9, 2017

letters is initially defined in string.py as letters = lowercase + uppercase. However, if your locale gets updated, then letters gets redefined in _localemodule.c to be a lexicographically ordered collection of characters for which isalpha(c) is true. Uppercase letters have a lower ordinal value than lowercase, so they come first. This is briefly touched upon in the docs although not in that much detail.

I don't know how/if help() is calling setlocale, though.

3.X's ascii_letters doesn't change, because it is not locale-dependent.

@tonnydourado
Copy link

On ipython, it is already reversed the first time, and calling help on string doesn't change it. Tested on python 2.7.6, ubuntu 14.04, locale en_US UTF-8, ipython 5.1.0

@mattboehm
Copy link
Author

@kms70847 I assume it has something to do with the fact that help sometimes launches a pager (i.e. less), but I haven't really looked into it yet.

@dschep
Copy link

dschep commented Mar 9, 2017

Furthermore:

[GCC 6.2.0 20160914] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import string
>>> id(string.letters)
140641450093232
>>> help(string)

>>> id(string.letters)
140641450093232

@dimo414
Copy link

dimo414 commented Mar 9, 2017

@dschep that's not so surprising, the new string.letters just happens to get the same ID, same as this question. If you assign the original string.letters to a variable the updated string.letters will have a different ID.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment