language-lawyer

Why don't callable attributes of a class become methods?

Why don't callable attributes of a class become methods? Question: Consider the following snippet. import types def deff(s): print(f"deff called, {s=}") lam = lambda s: print(f"lam called, {s=}") class Clbl: def __call__(s): print(f"__call__ called, {s=}") clbl = Clbl() print(type(deff) == types.FunctionType) # True print(type(lam) == types.LambdaType) # True print(type(clbl) == Clbl) # True class A: …

Total answers: 1

Is it legal to use more parameters than expected when calling a function?

Is it legal to use more parameters than expected when calling a function? Question: Context: I have written a Red Black tree implementation in C language. To allow it to use variable types, it only handles const void * elements, and initialisation of a tree must be given a comparison function with a signature int …

Total answers: 2

How is `x = 42; x = lambda: x` parsed?

How is `x = 42; x = lambda: x` parsed? Question: I was surprised that this assertion fails: x = 42 x = lambda: x assert x() == 42 It seems that x ends up recursively referring to itself, so that x(), x()(), etc. are all functions. What is the rule used to parse this, …

Total answers: 3

What does a star (asterisk) do in f-string?

What does a star (asterisk) do in f-string? Question: In the python document 2.4.3. Formatted string literals, it seems possible to write a star followed by an expression in a f-string’s {}, but I cannot find how to use that. What’s that and how I can use it? Is it documented somewhere? To be exact, …

Total answers: 2

Why are arbitrary target expressions allowed in for-loops?

Why are arbitrary target expressions allowed in for-loops? Question: I accidentally wrote some code like this: foo = [42] k = {‘c’: ‘d’} for k[‘z’] in foo: # Huh?? print k But to my surprise, this was not a syntax error. Instead, it prints {‘c’: ‘d’, ‘z’: 42}. My guess is that the code is …

Total answers: 4

Terminology: A user-defined function object attribute?

Terminology: A user-defined function object attribute? Question: According to Python 2.7.12 documentation, User-defined methods: User-defined method objects may be created when getting an attribute of a class (perhaps via an instance of that class), if that attribute is a user-defined function object, an unbound user-defined method object, or a class method object. When the attribute …

Total answers: 1

Why is a double semicolon a SyntaxError in Python?

Why is a double semicolon a SyntaxError in Python? Question: I know that semicolons are unnecessary in Python, but they can be used to cram multiple statements onto a single line, e.g. >>> x = 42; y = 54 I always thought that a semicolon was equivalent to a line break. So I was a …

Total answers: 2

Accessing attributes on literals work on all types, but not `int`; why?

Accessing attributes on literals work on all types, but not `int`; why? Question: I have read that everything in python is an object, and as such I started to experiment with different types and invoking __str__ on them — at first I was feeling really excited, but then I got confused. >>> “hello world”.__str__() ‘hello …

Total answers: 4