• 10
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

  • Name of the file from where code is running
  • Name of the class from where code is running
  • Name of the method (attribute of the class) where code is running

Here is an example of each:

from inspect import stack

class Foo:
    def __init__(self):
        print __file__
        print self.__class__.__name__
        print stack()[0][3]

f = Foo()
  • 31
Reply Report
self.__class__.__name__  # name of class i'm in

for the rest the sys and trace modules

http://docs.python.org/library/sys.html http://docs.python.org/library/trace.html

Some more info: https://mail.python.org/pipermail/python-list/2001-August/096499.html and http://www.dalkescientific.com/writings/diary/archive/2005/04/20/tracing_python_code.html

did you want it for error reporting because the traceback module can handle that:

http://docs.python.org/library/traceback.html

  • 5
Reply Report

Be very careful. Consider:

class A:
    pass

B = A
b = B()

What is the 'class name' of b here? Is it A, or B? Why?

The point is, you shouldn't need to know or care. An object is what it is: its name is very rarely useful.

  • 3
Reply Report
      • 2
    • To end users, surely; but extremely useful when debugging -- like for an exception handler or similar to report where it is. You could compose a nice error message to say the same thing, but it would frequently have this information in it anyway, in which case it seems a waste to type it every time. Also, to build (for example) a general logging package, it's immensely useful to be able to provide this automatically. If your argument held, wouldn't it also apply just as well to isinstance(), which surely is important and useful?

Trending Tags