SunQuest
           Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old November 26th, 2003, 12:45 PM
ilves ilves is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: Norway
Posts: 41 ilves User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 52 m 22 sec
Reputation Power: 6
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.
__________________
Good web hosting info - articles about web hosting
hb's web dev blog

Reply With Quote
  #2  
Old November 26th, 2003, 03:08 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #3  
Old November 26th, 2003, 03:42 PM
Igor Pechersky Igor Pechersky is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 28 Igor Pechersky User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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)

Reply With Quote
  #4  
Old November 26th, 2003, 05:40 PM
ilves ilves is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: Norway
Posts: 41 ilves User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 52 m 22 sec
Reputation Power: 6
Quote:
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

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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Accessing function/class name from within the function/class


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway