'Why class definition needs more information of its attributes than what is needed in a function definition?

def myfunc():
    return x
x = 10
print(myfunc())

The above codes work, where the free variable x does not need to be defined when I define myfunc.

However, the following codes do not work:

class myclass:
   func = extfunc

def extfunc(self):
    return 'hello'

Here I need to move the definition of extfunc before the class definition, in order to make the codes work.

Why does class definition need more information of its attributes than what is needed for a free variable in a function definition?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source