|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
Accessing function/class name from within the function/class
Hi
I have this code : Code:
class sky(object):
def sun(self):
return __name__
a = sky()
print a.sun()
print a.sun.__name__
print sky.__name__
It returns Code:
__main__ sun sky I would like the function sun to return sky.sun and preferably with any function arguments too. The function's name is stored in the __name__ attribute, but how do I get access to a functions __name__ attribute from within the function? I have tried func.__name__ etc but that won't compile. |
|
#2
|
||||
|
||||
|
Hey ilves
This was actually shockingly simple, more so than i imagined.. the class method doesn't even have to return anything if you don't wish! >>> class sky(object): def sun(self): None >>> print '%s.%s' % (sky.__name__, sky.sun.__name__) sky.sun >>> >>> #you can also do it like this, although i'd use the first.. >>> >>> print sky.__name__ + '.' + sky.sun.__name__ sky.sun >>> Ok so you see how to get and format the results.. heres you class where sun returns the data you wanted.. >>> class sky(object): def sun(self): return '%s.%s' % (sky.__name__, sky.sun.__name__) >>> sky().sun() 'sky.sun' >>> One question though, why are you doing this? particually why are you inheriting from 'object'.. Oh, just for your info you don't compile Python code ![]() Mark. |
|
#3
|
|||
|
|||
|
Yet another simple solution - though in opposite direction
Code:
class sky(object):
def sun(self):
return '%s.%s' % (self.__class__.__name__, sys._getframe().f_code.co_name)
Unlike netytan's solution, it is the true reflection (no hard-coded names of function and class) |
|
#4
|
|||
|
|||
|
Quote:
It's for testing/debugging. I want a piece of code I can paste into any function so I can see where (un)expected things happen. At the moment, I need it for a cgi-script (MVC architecture), so errors/exceptions/messages can be sent from a database connector class (model) via a Controller class to a View class where it is displayed. I'm just trying to be forward-compatible with the "new-style" classes, and this was just a made up class anyway. I know Python is an interpreted language, but in a way it is still compiled It's more work to hardcode the function names, and I might type the wrong function name, so I must avoid that. I found some useful functions in the inspect module and arrived at this solution. Just like what Igor suggested with the addition of the function arguments. Code:
a,b,c,d = inspect.getargvalues(inspect.currentframe()) debug = '%s.%s.%s' % (self.__class__.__name__, sys._getframe().f_code.co_name, inspect.formatargvalues(a,b,c,d)) It does the job now, but I will try to make it shorter and exclude the self argument to the function. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Accessing function/class name from within the function/class |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|