#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    Norway
    Posts
    41
    Rep Power
    12

    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
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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

  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    28
    Rep 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)
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    Norway
    Posts
    41
    Rep Power
    12
    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.
    Good web hosting info - articles about web hosting
    hb's web dev blog

IMN logo majestic logo threadwatch logo seochat tools logo