python-internals

Identifier normalization: Why is the micro sign converted into the Greek letter mu?

守給你的承諾、 提交于 2019-12-28 02:14:26
问题 I just stumbled upon the following odd situation: >>> class Test: µ = 'foo' >>> Test.µ 'foo' >>> getattr(Test, 'µ') Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> getattr(Test, 'µ') AttributeError: type object 'Test' has no attribute 'µ' >>> 'µ'.encode(), dir(Test)[-1].encode() (b'\xc2\xb5', b'\xce\xbc') The character I entered is always the µ sign on the keyboard, but for some reason it gets converted. Why does this happen? 回答1: There are two different characters

Why are Python dictionaries NOT stored in the order they were created? [duplicate]

烈酒焚心 提交于 2019-12-27 01:10:10
问题 This question already has answers here : Why is the order in dictionaries and sets arbitrary? (6 answers) Closed 4 years ago . Just curious more than anything else, but why isn't a dictionary such as the one below not ordered the same as it was created? But when I print out test it returns the same order from then on... test = {'one':'1', 'two':'2', 'three':'3', 'four':'4'} It's not that I need them ordered, but it's just been on my mind for awhile as to what is occurring here. The only thing

What is the default binding to the `__import__` attribute of the module `builtin`?

时光总嘲笑我的痴心妄想 提交于 2019-12-25 08:59:16
问题 From Python in a Nutshell Custom Importers An advanced, rarely needed functionality that Python offers is the ability to change the semantics of some or all import and from statements. Rebinding __import__ You can rebind the __import__ attribute of the module builtin to your own custom importer function—for example, one using the generic built-in-wrapping technique shown in “Python built-ins” on page 174. In "You can rebind the __import__ attribute of the module builtin ", should "the module

Why isn't my dict lookup faster than my list lookup in Python?

孤人 提交于 2019-12-25 05:11:48
问题 I'm reading each line of a file into both a list and a dict, with open("../data/title/pruned2_titleonly.txt", 'rb') as f_titles: titles_lst = f_titles.read().split('\n') assert titles_lst[-1] == '' titles_lst.pop() # remove the last element, an empty string titles_dict = {} with open("../data/title/pruned2_titleonly.txt", 'rb') as f_titles: for i,line in enumerate(f_titles): titles_dict[i] = line and I'm testing the performance by accessing each item in the list/dict in random order: n = len

Why don't tuples get the same ID when assigned the same values?

纵然是瞬间 提交于 2019-12-24 10:35:38
问题 When I executed the following steps, both tuples ( a and b ) haven't retained their original IDs even when I reassigned older values ( (1,2) ). >>> a , b = (1,2) , (1,2) >>> a (1, 2) >>> b (1, 2) >>> id(a) , id(b) (80131912, 91541064) >>> a , b = (3,4) , (3,4) >>> a (3, 4) >>> b (3, 4) >>> id(a) , id(b) (91559048, 91689032) >>> a , b = (1,2) , (1,2) >>> a (1, 2) >>> b (1, 2) >>> id(a) , id(b) (91556616, 91550408) But in the following case, both have gotten their older IDs back. >>> a = (1,2)

Is an explicit NUL-byte necessary at the end of a bytearray for cython to be able to convert it to a null-terminated C-string

孤人 提交于 2019-12-24 08:57:14
问题 When converting a bytearray -object (or a bytes -object for that matter) to a C-string, the cython-documentation recommends to use the following: cdef char * cstr = py_bytearray there is no overhead, as cstr is pointing to the buffer of the bytearray -object. However, C-strings are null-terminated and thus in order to be able to pass cstr to a C-function it must also be null-terminated. The cython-documentation doesn't provide any information, whether the resulting C-strings are null

Why does Python2.7 dict use more space than Python3 dict?

旧时模样 提交于 2019-12-23 10:06:05
问题 I've read about Raymond Hettinger's new method of implementing compact dicts. This explains why dicts in Python 3.6 use less memory than dicts in Python 2.7-3.5. However there seems to be a difference between the memory used in Python 2.7 and 3.3-3.5 dicts. Test code: import sys d = {i: i for i in range(n)} print(sys.getsizeof(d)) Python 2.7: 12568 Python 3.5: 6240 Python 3.6: 4704 As mentioned I understand the savings between 3.5 and 3.6 but am curious about the cause of the savings between

How is __subclasses__ method implemented in CPython?

吃可爱长大的小学妹 提交于 2019-12-23 09:49:21
问题 The docs say that: Each class keeps a list of weak references to its immediate subclasses. This method returns a list of all those references still alive. But how does each class obtain a list of weak references to its subclasses in the first place? In other words, when I create class B(A): pass how does A find out that B just subclassed it? And is this mechanism robust enough to survive edge cases (custom metaclasses, assignment to __bases__ , etc.)? 回答1: As part of the initialization of a

How big can the input to the input() function be?

允我心安 提交于 2019-12-23 06:45:50
问题 How large can the input I supply to the input() function be? Unfortunately, there was no easy way to test it. After using a lot of copy-pasting I couldn't get input to fail on any input I supplied. (and I eventually gave up) The documentation for the input function doesn't mention anything regarding this: If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing

How big can the input to the input() function be?

天大地大妈咪最大 提交于 2019-12-23 06:45:47
问题 How large can the input I supply to the input() function be? Unfortunately, there was no easy way to test it. After using a lot of copy-pasting I couldn't get input to fail on any input I supplied. (and I eventually gave up) The documentation for the input function doesn't mention anything regarding this: If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing