What does “del” do exactly?

Python is a garbage-collected language. If a value isn’t “reachable” from your code anymore, it will eventually get deleted. The del statement, as you saw, removes the binding of your variable. Variables aren’t values, they’re just names for values. If that variable was the only reference to the value anywhere, the value will eventually get … Read more

Tuple or list when using ‘in’ in an ‘if’ clause?

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 … Read more

Why is list(x for x in a) faster for a=[0] than for a=[]?

What you observe, is that pymalloc (Python memory manager) is faster than the memory manager provided by your C-runtime. It is easy to see in the profiler, that the main difference between both versions is that list_resize and _PyObjectRealloc need more time for the a=[]-case. But why? When a new list is created from an … Read more

How big can the input to the input() function be?

Of course there is, it can’t be limitless*. The key sentence from the documentation that I believe needs highlighting is: […] The function then reads a line from input, converts it to a string (stripping a trailing newline) […] (emphasis mine) Since it converts the input you supply into a Python str object it essentially … Read more

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

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 whitespace), possibly using different quoting conventions, are allowed, and their meaning is the same as their concatenation. Thus, “hello” ‘world’ is equivalent to “helloworld”. The compiled Python code sees … Read more

Why does list ask about __len__?

See the Rationale section from PEP 424 that introduced __length_hint__ and offers insight on the motivation: Being able to pre-allocate lists based on the expected size, as estimated by __length_hint__ , can be a significant optimization. CPython has been observed to run some code faster than PyPy, purely because of this optimization being present. In … Read more

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

Python separates the right-hand side expression from the left-hand side assignment. First the right-hand side is evaluated, and the result is stored on the stack, and then the left-hand side names are assigned using opcodes that take values from the stack again. For tuple assignments with 2 or 3 items, Python just uses the stack … Read more

Why are reversed and sorted of different types in Python?

The difference is that reversed is an iterator (it’s also lazy-evaluating) and sorted is a function that works “eagerly”. All built-in iterators (at least in python-3.x) like map, zip, filter, reversed, … are implemented as classes. While the eager-operating built-ins are functions, e.g. min, max, any, all and sorted. >>> a = [1,2,3,4] >>> r … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)