How does Python know whether a variable in the class is a method or a variable?

python

Solution

Things are wrapped into methods if they are functions (that is, their type is `types.FunctionType`).

This is because the function type defines a `__get__` method, implementing the descriptor protocol, which changes what happens when `A.b` is looked up. `int` and most other non-function callables do not define this method:

>>> (lambda x: x).__get__
<method-wrapper '__get__' of function object at 0x0000000003710198>
>>> int.__get__
Traceback (most recent call last):
  File "<pyshell#43>", line 1, in <module>
    int.__get__
AttributeError: type object 'int' has no attribute '__get__'

You could make your own method-wrapper-like behavior by defining some other sort of descriptor. An example of this is the `property`. `property` is a type that is not a function, but also defines a `__get__` (and `__set__`) to change what happens when a property is looked up.

Problem

``` print(hasattr(int, '__call__')) print(hasattr(lambda x: x, '__call__')) print('') class A(object): a = int b = lambda x : x print(A.a) print(A.b) ``` results in ``` True True <type 'int'> <unbound method A.<lambda>> ``` How does Python decide what is going to be a method (as `A.b` is here) and what is just going to be itself (as `A.a` is here)?

Original source