Python Forum
Please explain uncommon way of declaring and using variable [function.variable] - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Please explain uncommon way of declaring and using variable [function.variable] (/thread-30799.html)



Please explain uncommon way of declaring and using variable [function.variable] - esphi - Nov-06-2020

I came across similar code as bellow. It declare a variable foo.y and foo.z. They can be called outside the function.
I don't see this being explained in any tutorial.

Is this a common practice?
Can anyone please explain or give any lead on manual or info about this, thank you.

def foo(x):
    foo.y = 2
    foo.z = foo.y ** x 
    return x * foo.y

print(f'foo(3) is {foo(3)}')
print(f'foo.y is {foo.y}')
print(f'foo.z is {foo.z}')



Output:
foo(3) is 6 foo.y is 2 foo.z is 8



RE: Please explain uncommon way of declaring and using variable [function.variable] - perfringo - Nov-06-2020

These are function attributes and described in PEP 232


RE: Please explain uncommon way of declaring and using variable [function.variable] - deanhystad - Nov-06-2020

I knew this could be done (python functions have a __dict__), but I have never seen it done. I also cannot think of how I could use a function attribute and have never seen an example that wasn't contrived. But somebody must have seen a need and offered a compelling argument as to why function attributes were needed.
def sqrt(x):
    sqrt.x = x
    return x**0.5

print(sqrt(4))
sqrt.y = 'Hello'

for a in sqrt.__dict__:
    print(a, sqrt.__dict__[a])
Output:
2.0 x 4 y Hello



RE: Please explain uncommon way of declaring and using variable [function.variable] - esphi - Nov-07-2020

Thanks for the replies.
In other words these are function attributes, they do not behave exactly like a variable, and they are not suppose to be used as variable in general?


RE: Please explain uncommon way of declaring and using variable [function.variable] - buran - Nov-07-2020

I think it's important to say that in python we have first-class functions, i.e. functions are [first-class] object like any other and they can have attributes, incl. can have custom attributes.
Normally, plain names/variables would do just fine (e.g. in the example in the first post of the thread), but there are use cases where having custom attributes come handy.
Even without custom attributes. functions have plenty of attributes available to you

def spam():
    """Doc string for function spam()

    This is some dummy function
    """
    pass

print(spam.__doc__)
print(dir(spam))
Output:
(sbox) boyan@buranhome ~/sandbox2 $ /home/boyan/sandbox2/sbox/bin/python /home/boyan/sandbox2/forum.py Doc string for function spam() This is some dummy function ['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']