Questions about the internals of Python, like the data structures used, algorithms used, design decisions made. Basically this tag is will give answers to how Python works.

learn more… | top users | synonyms

9
votes
2answers
100 views

Python string with space and without space at the end and immutability

I learnt that in immutable classes, __new__ may return a cached reference to an existing object with the same value; this is what the int, str and tuple types do for small values. This is one of the ...
0
votes
3answers
60 views

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

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] == ...
8
votes
1answer
93 views

where is “from __future__ import braces” code

I was wondering what is exactly the code that executed on the command: >>> from __future__ import braces SyntaxError: not a chance so, since python is open-sourced I opened ...
20
votes
3answers
691 views

How print statement create a local variables

Question are at the end of this post. First snippet: empty local variable dictionary. def outer(): x = 1 def inner(): print "Local variables: %s" % locals() return inner() print ...
13
votes
2answers
175 views

What does “del” do exactly?

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 ...
3
votes
3answers
67 views

How does swapping of members in the python tuples (a,b)=(b,a) work internally?

In [55]: a = 5 In [56]: b = 6 In [57]: (a, b) = (b, a) In [58]: a Out[58]: 6 In [59]: b Out[59]: 5 How does this swapping of values of a and b work internally? Its definitely not using a temp ...
8
votes
2answers
87 views

Python method accessor creates new objects on each access?

When investigating for another question, I found the following: >>> class A: ... def m(self): return 42 ... >>> a = A() This was expected: >>> A.m == A.m True ...
2
votes
2answers
26 views

Python property instance behavior

I know what is property/descriptor and decorator. But I have some hard time getting this point. class Person(object): @property def name(self, func): pass print("-- before ...
7
votes
4answers
123 views

Implementation of NoneType, Reasons and Details

I recently read somewhere that the special value None in python is a singleton object of its own class, specifically NoneType. This explained a lot, since most errors involving None in python produce ...
4
votes
3answers
86 views

Writing (and not) to global variable in Python

Coming from much less dynamic C++, I have some trouble understanding the behaviour of this Python (2.7) code. Note: I am aware that this is bad programming style / evil, but I would like to ...
14
votes
1answer
129 views

What method does Python 2 use to print tuples?

Python's print statement normally seems to print the repr() of its input. Tuples don't appear to be an exception: >>> print (1, 2, 3) (1, 2, 3) >>> print repr((1, 2, 3)) (1, 2, 3) ...
5
votes
3answers
81 views

How does Python's list.remove(value) determine what value to remove?

If I have list of objects: l = [a, b, c] Then I remove one of those objects: l.remove(a) How is python determining which item in the list to remove (under the hood)? Is it using the memory ...
3
votes
1answer
106 views

Is python2 reluctant to free memory?

I know that python has its own memory management implementation using areans for objects of different sizes and much more, although I haven't found a thorough documentation yet. Still I'd like to ...
5
votes
4answers
123 views

Two instances of the same Python module?

I created a Python module with a single function that just prints 'a!'. I opened up the Python interpreter and imported the module in 2 different syntaxes >>> import a >>> from a ...
10
votes
1answer
82 views

Write a generator or return a generator?

Inside a container class, when I want to iterate over its items (or transformations of its items, or a subset of its items), I can either write a generator (like f) or return a generator (like g): ...
6
votes
3answers
90 views

Python swapping lists

In python, when I assign a list to another, like: a = [1,2,3] b = a Now b and a point to the same list. Now considering two lists, a = [1,2,3] b = [4,5,6] a,b = b,a Now how is it that they are ...
12
votes
4answers
268 views

Does Slicing `a` (e.g. `a[1:] == a[:-1]`) create copies of the `a`?

A friend of mine showed me the following Python code: a[1:] == a[:-1] Which returns True iff all the items in a are identical. I argued the code is hard to understand from first sight, and ...
28
votes
3answers
1k views

How references to variables are resolved in Python

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 ...
2
votes
4answers
49 views

Why aren't CPython Dicts affected by Hash Values for Negative one and negative two

Hash tables are supposed be high-performance mappings, and because Python dicts are implemented with hash tables, they're also highly performant. But I've encountered a strange result when looking at ...
1
vote
2answers
83 views

How to call tempfile.mkstemp() with “with”? - or why doesn't it return an fd with __exit__()?

To me the most idiomatic way of calling tempfile.mkstemp() would be as: with tempfile.mkstemp() as fd, filename: pass however, this obviously(?) raises AttributeError: __exit__ Calling ...
18
votes
3answers
6k views

Python, how does decorator @property work?

I would like to understand how buildin function property works. The confusing part for me is that property can be a decorator as well while it does not have arguments for decorating function. This ...
1
vote
3answers
106 views

How str implemented in python?

>>> import sys >>> sys.getsizeof("") 40 Why does empty string consist of so many bytes? Does anybody know what stored in those 40 bytes?
3
votes
2answers
149 views

Python, what is the object method of built-in id()?

In Python: len(a) can be replaced by a.__len__() str(a) or repr(a) can be replaced by a.__str__() or a.__repr__() == is __eq__, + is __add__, etc. Is there similar method to get the id(a) ? If ...
22
votes
4answers
608 views

Why must Python list addition be homogenous?

Can anyone familiar with Python's internals (CPython, or other implementations) why list addition is required to be homogenous: In [1]: x = [1] In [2]: x+"foo" ...
0
votes
1answer
37 views

Strange co_filename for file from .egg during tracing in Python 2.7

When tracing(using sys.settrace) python .egg execution by Python 2.7 interpreter frame.f_code.co_filename instead of <path-to-egg>/<path-inside-egg> eqauls to something like ...
1
vote
2answers
165 views

Synthetic functions in python

In python I can create a class without class statement: MyClass = type('X', (object,), dict(a=1)) Is there a way to create a function without 'def'? Thats as far as i got... d={} # func from ...
14
votes
3answers
428 views

'order' of unordered Python sets

Question from a noob (me): I understand that sets in Python are unordered, but I'm curious about the 'order' they're displayed in, as it seems to be consistent. They seem to be out-of-order in the ...
2
votes
2answers
259 views

Python: How to get information from a variable [closed]

Ok, this is maybe a difficult question. And I don't want you to do all the hard work for me. I'm just tring to get some good advices and points where I should start. I'm writing a couple of python ...
4
votes
2answers
187 views

Why is creating a set from a filter so much faster than creating a list or a tuple?

I’m running filter on an interable and want to store the result in a sequence (I need a sequence so that I can use random.choice on it). I noticed that creating a set from a filter object is a lot ...
1
vote
1answer
134 views

What is the best book/sources to learn the language design and internals of Python? [closed]

I have been using Python for around 1.5 yrs now and wish to understand how things are implemented. If no books exist for this, what would be the best approach to get a deeper understanding of the ...
0
votes
2answers
87 views

Why is Python more strict with circular imports when using from-imports?

I know that Python discourages any situation which can get you into a circular import. But I wanted understand the Python internals of why from-imports are seemingly arbitrarily less forgiving than ...
11
votes
2answers
1k views

List lookup faster than tuple?

In the past, when I've needed array-like indexical  lookups in a tight loop, I usually use tuples, since they seem to be generally extremely performant (close to using just n-number of variables). ...
5
votes
1answer
206 views

Why does refs increase 2 for every new object in Python?

It is a little weird to me that the refs number in the interactive environment increases 2 after a new object is defined. I created only one object, isn't it? >>> v Traceback (most recent ...
11
votes
3answers
1k views

Determining if a given Python module is a built-in module

I am doing some parsing and introspection of various modules, but I don't want to parse built-in modules. Now, there is no special type for built-in modules like there is a types.BuiltinFunctionType, ...
6
votes
4answers
227 views

Accessing the name that an object being created is assigned to

I'm writing some code to determine the name that an object is assigned to. This is for general debugging work and to further familiarize myself with python internals. I have it structured as a class ...