python-internals

Accessing the list while being sorted

最后都变了- 提交于 2019-11-26 17:55:40
Can I access a list while it is being sorted in the list.sort() b = ['b', 'e', 'f', 'd', 'c', 'g', 'a'] f = 'check this' def m(i): print i, b, f return None b.sort(key=m) print b this returns b [] check this e [] check this f [] check this d [] check this c [] check this g [] check this a [] check this Note that individual items of list b is sent to function m . But at m the list b is empty, however it can see the variable f , which has same scope as list b . Why does function m print b as [] ? Hyperboreus Looking at the source code (of CPython, maybe different behaviour for other

Why does a class' body get executed at definition time?

别等时光非礼了梦想. 提交于 2019-11-26 17:51:05
In contrast to functions, a class' body is executed at definition time: class A(object): print 'hello' Out: hello Why is it the case? Is it related to @classmethod / @staticmethod methods and class attributes? Everything is executed at the module level when Python first imports a module. Function bodies (and generator expression bodies) are the exception here, not the rule. Python executes everything to create the objects contained in a module; like everything in Python, classes are objects, and so are functions. The only reason a class body uses a separate code object is because a class body

Why is True returned when checking if an empty string is in another?

蹲街弑〆低调 提交于 2019-11-26 17:46:22
问题 My limited brain cannot understand why this happens: >>> print '' in 'lolsome' True In PHP, a equivalent comparison returns false: var_dump(strpos('', 'lolsome')); 回答1: From the documentation: For the Unicode and string types, x in y is true if and only if x is a substring of y . An equivalent test is y.find(x) != -1 . Note, x and y need not be the same type; consequently, u'ab' in 'abc' will return True . Empty strings are always considered to be a substring of any other string, so "" in

When are .pyc files refreshed?

谁说我不能喝 提交于 2019-11-26 17:33:05
问题 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.

list() uses more memory than list comprehension

会有一股神秘感。 提交于 2019-11-26 17:23:18
So i was playing with list objects and found little strange thing that if list is created with list() it uses more memory, than list comprehension? I'm using Python 3.5.2 In [1]: import sys In [2]: a = list(range(100)) In [3]: sys.getsizeof(a) Out[3]: 1008 In [4]: b = [i for i in range(100)] In [5]: sys.getsizeof(b) Out[5]: 912 In [6]: type(a) == type(b) Out[6]: True In [7]: a == b Out[7]: True In [8]: sys.getsizeof(list(b)) Out[8]: 1008 From the docs : Lists may be constructed in several ways: Using a pair of square brackets to denote the empty list: [] Using square brackets, separating items

'is' operator behaves unexpectedly with floats

耗尽温柔 提交于 2019-11-26 17:13:16
问题 I came across a confusing problem when unit testing a module. The module is actually casting values and I want to compare this values. There is a difference in comparison with == and is (partly, I'm beware of the difference) >>> 0.0 is 0.0 True # as expected >>> float(0.0) is 0.0 True # as expected As expected till now, but here is my "problem": >>> float(0) is 0.0 False >>> float(0) is float(0) False Why? At least the last one is really confusing to me. The internal representation of float(0

Are list comprehensions syntactic sugar for `list(generator expression)` in Python 3?

有些话、适合烂在心里 提交于 2019-11-26 14:40:30
问题 In Python 3, is a list comprehension simply syntactic sugar for a generator expression fed into the list function? e.g. is the following code: squares = [x**2 for x in range(1000)] actually converted in the background into the following? squares = list(x**2 for x in range(1000)) I know the output is identical, and Python 3 fixes the surprising side-effects to surrounding namespaces that list comprehensions had, but in terms of what the CPython interpreter does under the hood, is the former

How is super() in Python 3 implemented?

南笙酒味 提交于 2019-11-26 14:35:46
问题 I'm wondering how is the new super in Python 3 implemented. This question was born in my head after I have made a small example and I got a strange error. I'm using Pyutilib Component architecture (PCA) and I've made my custom metaclass to drive the creation of another class: from pyutilib.component.core import implements, SingletonPlugin, PluginMeta, Interface class IPass(Interface): pass class __MetaPlugin(PluginMeta): def __new__(cls, name, baseClasses, classdict): print(cls, name,

What does “del” do exactly?

让人想犯罪 __ 提交于 2019-11-26 14:13:15
问题 Here is my code: from memory_profiler import profile @profile def mess_with_memory(): huge_list = range(20000000) del huge_list print "why this kolaveri di?" This is what the output is, when I ran it from interpreter: Line # Mem usage Increment Line Contents 3 7.0 MiB 0.0 MiB @profile 4 def mess_with_memory(): 5 6 628.5 MiB 621.5 MiB huge_list = range(20000000) 7 476.0 MiB -152.6 MiB del huge_list 8 476.0 MiB 0.0 MiB print "why this kolaveri di" If you notice the output, creating the huge

Python eval: is it still dangerous if I disable builtins and attribute access?

瘦欲@ 提交于 2019-11-26 12:58:40
问题 We all know that eval is dangerous, even if you hide dangerous functions, because you can use Python\'s introspection features to dig down into things and re-extract them. For example, even if you delete __builtins__ , you can retrieve them with [c for c in ().__class__.__base__.__subclasses__() if c.__name__ == \'catch_warnings\'][0]()._module.__builtins__ However, every example I\'ve seen of this uses attribute access. What if I disable all builtins, and disable attribute access (by