adding attributes to a function, from within the function

Sean Cody sean at -[NOSPAMPLEASE]-tfh.ca
Fri Oct 31 11:33:06 EST 2003


> What's the syntax to add an attribute to a function form the function
body?
>
I don't think this is entirely possible as there is no way obvious way to
introspect the members of a function based on the language (as I know it).

Functions can be invoked many times so you would not know which version of
the member variable you will get (assuming the python interpeter copies the
text segment of the function instead of branching to it [which is probably
the case and if so the text segment doesn't normally have 'user memory'
allocated within it as local variables may be on the stack [unless in python
even local statics are heap based]]).  Once a function has finished its
memory is free'd unless it is some sort of static function.

What you could do is:

class fn:
        __init__():
              self.error = -1
              function_stuff()

Then you could do it but it is more effort that it is worth...

>From an OO or even modular standpoint you shouldn't break scope unless there
is a really good reason to.

--
Sean
P.S. I'm probably totally wrong but if I didn't at least try I wouldn't
learn anything. :P
P.P.S. I've used way too many buzz words.... maybe I shouldn't post to
groups shortly after management meetings. :)






More information about the Python-list mailing list