0

When I create a function in python for example

def foo():
  pass

And when I type print(type(foo)) it returns class ‘function’,

so my question is if every function we create belongs to class ‘function’ why we print foo() instead of function.foo() to call this function?

Because if I create a class and add a method to call this method I would write classname.method()

sahasrara62
  • 10,069
  • 3
  • 29
  • 44
  • I suggest that you read [this](https://stackoverflow.com/a/865963/866527) answer and the article there. – nonDucor Jul 04 '22 at 12:52
  • The type is distinct from the namespace; you don't need `str."text"` either (though `str("text")` is possible if you really want to). – tripleee Jul 04 '22 at 12:52

1 Answers1

1

foo has type function; it is not an attribute of the function class.

More precisely, foo is a name in the global scope that is bound to a value of type function, rather than being a name in the in the namespace of the function class.

chepner
  • 497,756
  • 71
  • 530
  • 681