By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,406 Members | 2,660 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,406 IT Pros & Developers. It's quick & easy.

Add attributes to function in python

P: 5
Hi,

I would like to add some descriptions to functions on top of every function

ex :

[Name = 'Test menu', Default=True, etc..]
def test():
pass


My actual scenario is , I create Menus dynamically by reading all methods from a module at run time.

So i don't want to take menu name as test, i want user to provide menu name on his wish in "Name" attribute of a function. There will be more attribute, not only Name.
Is there any possibility in python, please anyone guide me ?
Jun 12 '14 #1
Share this Question
Share on Google+
1 Reply


bvdet
Expert Mod 2.5K+
P: 2,851
You can add attributes and associated values to function objects.
Expand|Select|Wrap|Line Numbers
  1. >>> def test():
  2. ...     print 'test'
  3. ...     
  4. >>> setattr(test, 'menu', 'bill')
  5. >>> test.menu
  6. 'bill'
I am wondering if function default arguments are what you really want.
Expand|Select|Wrap|Line Numbers
  1. >>> def test(name="Test Menu", default=True):
  2. ...     if default is True:
  3. ...         print "Name of menu: %s" % (name)
  4. ...         
  5. >>> test()
  6. Name of menu: Test Menu
  7. >>> 
Jun 12 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.