Skip to main content
deleted 16 characters in body
Source Link
Ned Batchelder
  • 378.1k
  • 77
  • 583
  • 675

How about a function you can call? This function accesses the caller's frame, and rather than using locals(), uses frame.f_locals to get the caller's namespace.

def my_function():
    frame = currentframe().f_back
    msg = 'We are on file {0.f_code.co_filename} and line {0.f_lineno}'.format(frameinfoframe)
    current_state = frameinfoframe.f_locals.items()
    print current_state['some_variable']

Then just call it:

def some_function:
    my_function()

def some_other_function:
    some_function()
    my_function()

class some_class:
  def some_method:
     my_function()

How about a function you can call?

def my_function():
    frame = currentframe().f_back
    msg = 'We are on file {0.f_code.co_filename} and line {0.f_lineno}'.format(frameinfo)
    current_state = frameinfo.f_locals.items()
    print current_state['some_variable']

Then just call it:

def some_function:
    my_function()

def some_other_function:
    some_function()
    my_function()

class some_class:
  def some_method:
     my_function()

How about a function you can call? This function accesses the caller's frame, and rather than using locals(), uses frame.f_locals to get the caller's namespace.

def my_function():
    frame = currentframe().f_back
    msg = 'We are on file {0.f_code.co_filename} and line {0.f_lineno}'.format(frame)
    current_state = frame.f_locals
    print current_state['some_variable']

Then just call it:

def some_function:
    my_function()

def some_other_function:
    some_function()
    my_function()

class some_class:
  def some_method:
     my_function()
Source Link
Ned Batchelder
  • 378.1k
  • 77
  • 583
  • 675

How about a function you can call?

def my_function():
    frame = currentframe().f_back
    msg = 'We are on file {0.f_code.co_filename} and line {0.f_lineno}'.format(frameinfo)
    current_state = frameinfo.f_locals.items()
    print current_state['some_variable']

Then just call it:

def some_function:
    my_function()

def some_other_function:
    some_function()
    my_function()

class some_class:
  def some_method:
     my_function()