0

I am a Java programmer making his way into Python, and some things are just baffling. For example, adding variables to functions, dynamically. For example:

def main():
    print ("Hello World")


main.temp = 50

print (main.temp)

What does that even mean? Why would you want to add a variable to a function? Why is that allowed? And why am I allowed to reference the function via main.temp?

12
  • 2
    Well, everything in python is an object, even functions. Commented May 4, 2018 at 11:58
  • 1
    @ng.newbie why forbid it ? Being able to annotate a function with any arbitrary attribute is actually quite useful (and indeed used by some well-known frameworks). Commented May 4, 2018 at 12:09
  • 1
    FWIW, I quite like function attributes, but I admit they aren't popular, and there are generally better ways to do stuff that you can do using function attributes. But IMHO they are a convenient way to attach data to a function, and they're better than having a stray object floating around in the global space that's supposed to be associated with the function. Commented May 4, 2018 at 12:17
  • 1
    Django templates (to prevent some methods from being called from a template), Django admin (to give meaningfull label to computed listview fields), I know I've seen it in other frameworks but can't remember which ATM, and I've personnaly used this in at least a dozen projects for similar reasons: adding meta data to a function so some other piece of code can apply special treatments to those functions. Commented May 4, 2018 at 12:23
  • 1
    Standard Python is often known as CPython, because the interpreter is written in C, and the built-in types it defines are defined using C structures. It's also possible to write your own extensions to Python in C, although that's not often done by mere mortals. ;) But take a look here for the C source of the standard list object. Commented May 4, 2018 at 12:26

1 Answer 1

1

Functions in Python are first-class objects which means you can do almost whatever you want to do with them. Heck, you can even do this:

def foo(): pass

foo.inner_foo = lambda: print('inner foo')

foo.inner_foo()
# inner foo

Yes, it is a function with an attribute which is a function. And yes, we can go deeper:

def foo(): pass

foo.inner_foo = lambda: None

foo.inner_foo.inner_inner_foo = lambda: print('inner inner foo')

foo.inner_foo.inner_inner_foo()
# inner inner foo

And yes, we can go deeper, but you get the idea...

Sign up to request clarification or add additional context in comments.

5 Comments

When was this ever helpful? Why allow this feature? Just because functions are objects you shouldn't be allowed to add variables to it.
Django makes extensive use of this. Just because you don't immediately see the utility does not make it worthless.
@ng.newbie "just because functions are objects you shouldn't be allowed to add variables to it." why? that is almost literally the definition of first-class objects. And yes, this is very useful, see the answers to the linked duplicated question.
@DeepSpace Objects in Java don't allow the addition of variables after it is created. So that's why I said what I said. Is the definition of the object different in Python?
@James Could you please give an example of how Django uses this? Maybe if I saw a real life warranted use, then maybe this feature would annoy me a little less.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.