473,402 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Add attributes to function in python

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
1 1645
bvdet
2,851 Expert Mod 2GB
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

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

Similar topics

6
by: beliavsky | last post by:
I have started using classes with Python and have a question about their use. In Python, 'attributes are all "public" and "virtual" in C++ terms; they're all accessible everywhere and all looked...
32
by: Kalle Anke | last post by:
I'm coming to Python from other programming languages. I like to hide all attributes of a class and to only provide access to them via methods. Some of these languages allows me to write something...
5
by: frankie_85 | last post by:
Ok I'm really lost (I'm new to python) how to use the reverse function. I made a little program which basically the a, b, c, d, e which I have listed below and basically I want it th result to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.