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

0 votes
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)

Gives an output of: 

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)?

Sep 18, 2018 in Python by charlie_brown
• 7,720 points
1,163 views

1 answer to this question.

0 votes

In python objects/variables are wrapped into methods if they are function (i.e type( xxx ) gives FunctionType)

This is because the FunctionType 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

answered Sep 18, 2018 by aryya
• 7,460 points

Related Questions In Python

0 votes
1 answer

How to check whether something is a variable or a function in Python?

Suppose you have a variable defined in ...READ MORE

answered Jul 30, 2019 in Python by Arvind
• 3,050 points
1,931 views
0 votes
0 answers

How to check whether a variable is a class or not?

I was wondering how to check whether ...READ MORE

Nov 24, 2022 in Python by sarit
• 1,830 points
421 views
0 votes
1 answer

How do I use urllib to see if a website is 404 or 200 in Python?

For Python 3, try doing this: import urllib.request, ...READ MORE

answered Nov 29, 2018 in Python by Nymeria
• 3,560 points

edited Dec 11, 2018 by Nymeria 14,028 views
0 votes
1 answer

How do I check if input string is a valid regular expression or not in Python?

Hi. Good question! Well, just like what ...READ MORE

answered Feb 12, 2019 in Python by Nymeria
• 3,560 points
11,286 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,070 views
0 votes
1 answer

How to get the size of a string in Python?

If you are talking about the length ...READ MORE

answered Jun 4, 2018 in Python by aryya
• 7,460 points
1,373 views
0 votes
1 answer

How can I prevent or alter access to class variables in Python?

The ActiveState solution that Pynt references makes instances of ...READ MORE

answered Dec 5, 2018 in Python by aryya
• 7,460 points
3,236 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP