Reputation: 63
I've written a piece of code:
def Greeting():
return "Hello there!"
Greeting.help = "This function will say hello"
print(Greeting())
print(Greeting.help)
What I am unsure what the Greeting.help would be called... I've tried searching but i feel like i'm using the wrong search terms.
Upvotes: 2
Views: 121
Reputation: 11940
You've set a single attribute of an object (a function object in this case).
If you wanted to document it, then a more traditional way is to set the docstring:
def Greeting():
""" This function will say hello """
return "Hello there!"
then it can be viewed via help(Greeting)
:
>>> def Greeting():
... """ This function will say hello """
... return "Hello there!"
...
>>> Greeting.__doc__
' This function will say hello '
>>> help(Greeting)
It prints:
Help on function Greeting in module __main__:
Greeting()
This function will say hello
(END)
Upvotes: 2
Reputation: 78690
You have set an attribute on the object Greeting
. That's why the corresponding functions are called getattr
and setattr
.
>>> getattr(Greeting, 'help')
'This function will say hello'
>>> setattr(Greeting, 'foo', 'bar')
These attributes are stored within the dictionary Greeting.__dict__
, which can also be accessed by vars(Greeting)
.
>>> Greeting.__dict__
{'foo': 'bar', 'help': 'This function will say hello'}
>>> vars(Greeting)
{'foo': 'bar', 'help': 'This function will say hello'}
Note that the idiomatic way to set a help/docstring goes as follows:
>>> def foo():
... 'help text'
... pass
...
>>> foo.__doc__
'help text'
Upvotes: 3