python-internals

Dict/Set Parsing Order Consistency

余生长醉 提交于 2019-11-27 06:36:51
问题 Containers that take hashable objects (such as dict keys or set items). As such, a dictionary can only have one key with the value 1 , 1.0 or True etc. (note: simplified somewhat - hash collisions are permitted, but these values are considered equal) My question is: is the parsing order well-defined and is the resulting object predictable across implementations? For example, OSX Python 2.7.11 and 3.5.1 interprets dict like so: >>> { True: 'a', 1: 'b', 1.0: 'c', (1+0j): 'd' } {True: 'd'} In

What is under the hood of x = 'y' 'z' in Python?

别说谁变了你拦得住时间么 提交于 2019-11-27 06:34:04
问题 If you run x = 'y' 'z' in Python, you get x set to 'yz' , which means that some kind of string concatenation is occurring when Python sees multiple strings next to each other. But what kind of concatenation is this? Is it actually running 'y' + 'z' or is it running ''.join('y','z') or something else? 回答1: The Python parser interprets that as one string. This is well documented in the Lexical Analysis documentation: String literal concatenation Multiple adjacent string literals (delimited by

Unexpected value from sys.getrefcount

徘徊边缘 提交于 2019-11-27 06:21:23
问题 Under Python 2.7.5 >>> import sys >>> sys.getrefcount(10000) 3 Where are the three refcount? PS: when the 10000 PyIntObject would be Py_DECREF to 0 ref and deallocated? Do not say about gc stuff, reference count itself can work without gc. 回答1: When you do something in the REPL console, the string will be compiled internally and during the compilation process, Python creates an intermediate list with the list of strings apart from tokens. So, that is reference number 1. You can check this

Why is a function/method call in python expensive?

半城伤御伤魂 提交于 2019-11-27 06:11:40
问题 In this post, Guido van Rossum says that a function call may be expensive, but I do not understand why nor how much expensive can be. How much delay adds to your code a simple function call and why? 回答1: A function call requires that the current execution frame is suspended, and a new frame is created and pushed on the stack. This is relatively expensive, compared to many other operations. You can measure the exact time required with the timeit module: >>> import timeit >>> def f(): pass ...

How do chained comparisons in Python actually work?

牧云@^-^@ 提交于 2019-11-27 06:00:57
问题 The Python Doc for Comparisons says: Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z , except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false). And these SO questions/answers shed some more light on such usage: Python comparison operators chaining/grouping left to right? What does "evaluated only once" mean for chained comparisons in Python?, in particular the currently-accepted answer So

How does __call__ actually work?

不羁的心 提交于 2019-11-27 06:00:47
问题 Python's magic method __call__ is called whenever you attempt to call an object. Cls()() is thus equal to Cls.__call__(Cls()) . Functions are first class objects in Python, meaning they're just callable objects (using __call__ ). However, __call__ itself is a function, thus it too has __call__ , which again has its own __call__ , which again has its own __call__ . So Cls.__call__(Cls()) is thus equal to Cls.__call__.__call__(Cls()) and again equilevant to Cls.__call__.__call__.__call__(Cls())

Converting a series of ints to strings - Why is apply much faster than astype?

帅比萌擦擦* 提交于 2019-11-27 05:33:12
问题 I have a pandas.Series containing integers, but I need to convert these to strings for some downstream tools. So suppose I had a Series object: import numpy as np import pandas as pd x = pd.Series(np.random.randint(0, 100, 1000000)) On StackOverflow and other websites, I've seen most people argue that the best way to do this is: %% timeit x = x.astype(str) This takes about 2 seconds. When I use x = x.apply(str) , it only takes 0.2 seconds. Why is x.astype(str) so slow? Should the recommended

What are __signature__ and __text_signature__ used for in Python 3.4

不羁岁月 提交于 2019-11-27 04:52:26
If one does dir() on some builtin callables (class constructors, methods, etc) on CPython 3.4, one finds out that many of them often have a special attribute called __text_signature__ , for example: >>> object.__text_signature__ '()' >>> int.__text_signature__ >>> # was None However the documentation for this is nonexistent. Furthermore, googling for the attribute name suggests that there is also another possible special attribute __signature__ , though I did not find any built-in functions that would have it. I do know they are related to the function argument signature, but nothing beyond

Python string literal concatenation

南楼画角 提交于 2019-11-27 04:46:47
问题 I can create a multi-line string using this syntax: string = str("Some chars " "Some more chars") This will produce the following string: "Some chars Some more chars" Is Python joining these two separate strings or is the editor/compiler treating them as a single string? P.s: I just want to understand the internals. I know there are other ways to declare or create multi-line strings. 回答1: Read the reference manual, it's in there . Specifically: Multiple adjacent string or bytes literals

Is there anything faster than dict()?

佐手、 提交于 2019-11-27 04:30:55
I need a faster way to store and access around 3GB of k:v pairs. Where k is a string or an integer and v is an np.array() that can be of different shapes. Is there any object, that is faster than the standard python dict in storing and accessing such a table? For example, a pandas.DataFrame ? As far I have understood python dict is a quite fast implementation of a hashtable, is there anything better than that for my specific case? No there is nothing faster than a dictionary for this task and that’s because the complexity of its indexing and even membership checking is approximately O(1). Once