language-design

design of python: why is assert a statement and not a function?

design of python: why is assert a statement and not a function? Question: In Python, assert is a statement, and not a function. Was this a deliberate decision? Are there any advantages to having assert be a statement (and reserved word) instead of a function? According to the docs, assert expression1, expression2 is expanded to …

Total answers: 4

Why are slice and range upper-bound exclusive?

Why are slice and range upper-bound exclusive? Question: I know that when I use range([start], stop[, step]) or slice([start], stop[, step]), the stop value is not included in the range or slice. But why does it work this way? Is it so that e.g. a range(0, x) or range(x) will contain x many elements? Is …

Total answers: 6

What's the difference between __builtin__ and __builtins__?

What's the difference between __builtin__ and __builtins__? Question: I was coding today and noticed something. If I open a new interpreter session (IDLE) and check what’s defined with the dir function I get this: $ python >>> dir() [‘__builtins__’, ‘__doc__’, ‘__name__’, ‘__package__’] >>> dir(__builtins__) [‘ArithmeticError’, ‘AssertionError’, ‘AttributeError’, ‘BaseException’, ‘BufferError’, ‘BytesWarning’, ‘DeprecationWarning’, ‘EOFError’, ‘Ellipsis’, ‘EnvironmentError’, ‘Exception’, …

Total answers: 5

Which classes cannot be subclassed?

Which classes cannot be subclassed? Question: Is there any rule about which built-in and standard library classes are not subclassable (“final”)? As of Python 3.3, here are a few examples: bool function operator.itemgetter slice I found a question which deals with the implementation of “final” classes, both in C and pure Python. I would like …

Total answers: 2

Why can't I add a tuple to a list with the '+' operator in Python?

Why can't I add a tuple to a list with the '+' operator in Python? Question: Python not support adding a tuple to a list: >>> [1,2,3] + (4,5,6) Traceback (most recent call last): File “<stdin>”, line 1, in <module> TypeError: can only concatenate list (not “tuple”) to list What are the disadvantages for providing …

Total answers: 3

Why isn't __new__ in Python new-style classes a class method?

Why isn't __new__ in Python new-style classes a class method? Question: The Changelog for Python 2.2 (where new-style classes were introduced) says the following about the __new__ function: __new__ is a static method, not a class method. I initially thought it would have to be a class method, and that’s why I added the classmethod …

Total answers: 1

Why are slices in Python 3 still copies and not views?

Why are slices in Python 3 still copies and not views? Question: As I only now noticed after commenting on this answer, slices in Python 3 return shallow copies of whatever they’re slicing rather than views. Why is this still the case? Even leaving aside numpy’s usage of views rather than copies for slicing, the …

Total answers: 2

Why are there no sorted containers in Python's standard libraries?

Why are there no sorted containers in Python's standard libraries? Question: Is there a Python design decision (PEP) that precludes a sorted container from being added to Python? (OrderedDict is not a sorted container since it is ordered by insertion order.) Asked By: Neil G || Source Answers: There is a heapq in the standard …

Total answers: 6

What blocks Ruby, Python to get Javascript V8 speed?

What blocks Ruby, Python to get Javascript V8 speed? Question: Are there any Ruby / Python features that are blocking implementation of optimizations (e.g. inline caching) V8 engine has? Python is co-developed by Google guys so it shouldn’t be blocked by software patents. Or this is rather matter of resources put into the V8 project …

Total answers: 11