python-internals

Tuple or list when using 'in' in an 'if' clause?

≡放荡痞女 提交于 2019-11-27 04:23:28
Which approach is better? Using a tuple, like: if number in (1, 2): or a list, like: if number in [1, 2]: Which one is recommended for such uses and why (both logical and performance wise)? The CPython interpreter replaces the second form with the first . That's because loading the tuple from a constant is one operation, but the list would be 3 operations; load the two integer contents and build a new list object. Because you are using a list literal that isn't otherwise reachable, it is substituted for a tuple: >>> import dis >>> dis.dis(compile('number in [1, 2]', '<stdin>', 'eval')) 1 0

How is tuple implemented in CPython?

心已入冬 提交于 2019-11-27 04:04:14
I've been trying to learn how CPython is implemented under the scenes. It's great that Python is high level, but I don't like treating it like a black box. With that in mind, how are tuples implemented? I've had a look at the source (tupleobject.c) , but it's going over my head. I see that PyTuple_MAXSAVESIZE = 20 and PyTuple_MAXFREELIST = 2000 , what is saving and the "free list"? (Will there be a performance difference between tuples of length 20/21 or 2000/2001? What enforces the maximum tuple length?) As a caveat, everything in this answer is based on what I've gleaned from looking over

list comprehension filtering - “the set() trap”

自古美人都是妖i 提交于 2019-11-27 03:52:40
A reasonably common operation is to filter one list based on another list . People quickly find that this: [x for x in list_1 if x in list_2] is slow for large inputs - it's O(n*m). Yuck. How do we speed this up? Use a set to make filtering lookups O(1): s = set(list_2) [x for x in list_1 if x in s] This gives nice overall O(n) behavior. I however often see even veteran coders fall into The Trap ™: [x for x in list_1 if x in set(list_2)] Ack! This is again O(n*m) since python builds set(list_2) every time, not just once. I thought that was the end of the story - python can't optimize it away

Why does + (plus) can concatenate two strings in Python?

萝らか妹 提交于 2019-11-27 03:49:41
问题 I'm learning Learn Python the hard way. w = "This is the left side of..." e = "a string with a right side." print w + e Explain why adding the two strings w and e with + makes a longer string. Even I know it can work, but I don't understand why and how? Please help me. 回答1: Python uses + to concatenate strings because that's how core developers of Python defined that operator. While it's true that __add__ special method is normally used to implement the + operator, + (BINARY_ADD bytecode

When are .pyc files refreshed?

跟風遠走 提交于 2019-11-27 03:35:20
I understand that ".pyc" files are compiled versions of the plain-text ".py" files, created at runtime to make programs run faster. However I have observed a few things: Upon modification of "py" files, program behavior changes. This indicates that the "py" files are compiled or at least go though some sort of hashing process or compare time stamps in order to tell whether or not they should be re-compiled. Upon deleting all ".pyc" files ( rm *.pyc ) sometimes program behavior will change. Which would indicate that they are not being compiled on update of ".py"s. Questions: How do they decide

Why is variable1 += variable2 much faster than variable1 = variable1 + variable2?

一笑奈何 提交于 2019-11-27 03:29:55
I have inherited some Python code which is used to create huge tables (of up to 19 columns wide by 5000 rows). It took nine seconds for the table to be drawn on the screen. I noticed that each row was added using this code: sTable = sTable + '\n' + GetRow() where sTable is a string. I changed that to: sTable += '\n' + GetRow() and I noticed that the table now appeared in six seconds . And then I changed it to: sTable += '\n%s' % GetRow() based on these Python performance tips (still six seconds). Since this was called about 5000 times, it highlighted the performance issue. But why was there

Why is the __dict__ of instances so much smaller in size in Python 3?

落花浮王杯 提交于 2019-11-27 03:13:20
问题 In Python, dictionaries created for the instances of a class are tiny compared to the dictionaries created containing the same attributes of that class: import sys class Foo(object): def __init__(self, a, b): self.a = a self.b = b f = Foo(20, 30) When using Python 3.5.2, the following calls to getsizeof produce: >>> sys.getsizeof(vars(f)) # vars gets obj.__dict__ 96 >>> sys.getsizeof(dict(vars(f)) 288 288 - 96 = 192 bytes saved! Using Python 2.7.12, though, on the other hand, the same calls

Why None is the smallest in python? [duplicate]

醉酒当歌 提交于 2019-11-27 03:06:56
问题 This question already has an answer here: Is everything greater than None? 2 answers What I learnt from python None : None is frequently used to represent the absence of a value When i put in a list and sorted with numbers and string. I got the following result, which means it is the smallest number ? Reverse: >>> sorted([1, 2, None, 4.5, (-sys.maxint - 1), (sys.maxint - 1), 'abc'], reverse=True) ['abc', 9223372036854775806, 4.5, 2, 1, -9223372036854775808, None] >>> Normal sort: >>> sorted(

How references to variables are resolved in Python

五迷三道 提交于 2019-11-27 02:52:33
This message is a a bit long with many examples, but I hope it will help me and others to better grasp the full story of variables and attribute lookup in Python 2.7. I am using the terms of PEP 227 ( http://www.python.org/dev/peps/pep-0227/ ) for code blocks (such as modules, class definition, function definitions, etc.) and variable bindings (such as assignments, argument declarations, class and function declaration, for loops, etc.) I am using the terms variables for names that can be called without a dot, and attributes for names that need to be qualified with an object name (such as obj.x

Runtime of python's if substring in string

岁酱吖の 提交于 2019-11-27 02:47:33
问题 What is the big O of the following if statement ? if "pl" in "apple": ... What is the overall big O of how python determines if the string "pl" is found in the string "apple" or any other substring in string search. Is this the most efficient way to test if a substring is in a string? Does it use the same algorithm as .find() ? 回答1: In python 3.4.2 it looks like they are resorting to the same function, but there may be difference in timing nevertheless. For example s.find first is required to