python-internals

Finding the source code for built-in Python functions?

∥☆過路亽.° 提交于 2019-11-25 22:36:36
问题 Is there a way to see how built in functions work in python? I don\'t mean just how to use them, but also how were they built, what is the code behind sorted or enumerate etc...? 回答1: Since Python is open source you can read the source code. To find out what file a particular module or function is implemented in you can usually print the __file__ attribute. Alternatively, you may use the inspect module, see the section Retrieving Source Code in the documentation of inspect . For built-in

What is the global interpreter lock (GIL) in CPython?

妖精的绣舞 提交于 2019-11-25 22:20:02
问题 What is a global interpreter lock and why is it an issue? A lot of noise has been made around removing the GIL from Python, and I\'d like to understand why that is so important. I have never written a compiler nor an interpreter myself, so don\'t be frugal with details, I\'ll probably need them to understand. 回答1: Python's GIL is intended to serialize access to interpreter internals from different threads. On multi-core systems, it means that multiple threads can't effectively make use of

Accessing class variables from a list comprehension in the class definition

浪尽此生 提交于 2019-11-25 22:15:53
问题 How do you access other class variables from a list comprehension within the class definition? The following works in Python 2 but fails in Python 3: class Foo: x = 5 y = [x for i in range(1)] Python 3.2 gives the error: NameError: global name \'x\' is not defined Trying Foo.x doesn\'t work either. Any ideas on how to do this in Python 3? A slightly more complicated motivating example: from collections import namedtuple class StateDatabase: State = namedtuple(\'State\', [\'name\', \'capital\'

What's with the integer cache maintained by the interpreter?

╄→гoц情女王★ 提交于 2019-11-25 21:56:54
问题 After dive into Python\'s source code, I find out that it maintains an array of PyInt_Object s ranging from int(-5) to int(256) (@src/Objects/intobject.c) A little experiment proves it: >>> a = 1 >>> b = 1 >>> a is b True >>> a = 257 >>> b = 257 >>> a is b False But if I run those code together in a py file (or join them with semi-colons), the result is different: >>> a = 257; b = 257; a is b True I\'m curious why they are still the same object, so I digg deeper into the syntax tree and

Usage of __slots__?

只谈情不闲聊 提交于 2019-11-25 21:53:38
问题 What is the purpose of __slots__ in Python — especially with respect to when I would want to use it, and when not? 回答1: In Python, what is the purpose of __slots__ and what are the cases one should avoid this? TLDR: The special attribute __slots__ allows you to explicitly state which instance attributes you expect your object instances to have, with the expected results: faster attribute access. space savings in memory. The space savings is from Storing value references in slots instead of _

Why is the order in dictionaries and sets arbitrary?

对着背影说爱祢 提交于 2019-11-25 21:35:48
问题 I don\'t understand how looping over a dictionary or set in python is done by \'arbitrary\' order. I mean, it\'s a programming language so everything in the language must be 100% determined, correct? Python must have some kind of algorithm that decides which part of the dictionary or set is chosen, 1st, second and so on. What am I missing? 回答1: The order is not arbitrary, but depends on the insertion and deletion history of the dictionary or set, as well as on the specific Python

“is” operator behaves unexpectedly with integers

…衆ロ難τιáo~ 提交于 2019-11-25 21:33:40
问题 Why does the following behave unexpectedly in Python? >>> a = 256 >>> b = 256 >>> a is b True # This is an expected result >>> a = 257 >>> b = 257 >>> a is b False # What happened here? Why is this False? >>> 257 is 257 True # Yet the literal numbers compare properly I am using Python 2.5.2. Trying some different versions of Python, it appears that Python 2.3.3 shows the above behaviour between 99 and 100. Based on the above, I can hypothesize that Python is internally implemented such that \