Python Forum
Please explain uncommon way of declaring and using variable [function.variable]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please explain uncommon way of declaring and using variable [function.variable]
#1
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
Reply
#2
These are function attributes and described in PEP 232
esphi likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
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
esphi likes this post
Reply
#4
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?
Reply
#5
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__']
esphi likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Commas issue in variable ddahlman 6 368 Apr-05-2024, 03:45 PM
Last Post: deanhystad
  Variable Explorer in spyder driesdep 1 184 Apr-02-2024, 06:50 AM
Last Post: paul18fr
  Mediapipe. Not picking up second variable stevolution2024 1 159 Mar-31-2024, 05:56 PM
Last Post: stevolution2024
Question Variable not defined even though it is CoderMerv 3 231 Mar-28-2024, 02:13 PM
Last Post: Larz60+
  optimum chess endgame with D=3 pieces doesn't give an exact moves_to_mate variable max22 1 235 Mar-21-2024, 09:31 PM
Last Post: max22
  unbounded variable akbarza 3 475 Feb-07-2024, 03:51 PM
Last Post: deanhystad
  Variable for the value element in the index function?? Learner1 8 619 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Variable definitions inside loop / could be better? gugarciap 2 417 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  working directory if using windows path-variable chitarup 2 719 Nov-28-2023, 11:36 PM
Last Post: chitarup
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 567 Nov-23-2023, 02:53 PM
Last Post: rob101

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020