简体   繁体   English

允许调用函数在Python中获取调用者的属性

[英]Allow calling function to get caller's attribute in Python

I want to create a function that will be called whenever the caller gets arguments of wrong instance, that will print caller's __doc__ attribute and exit. 我想创建一个在调用者获取错误实例参数时将被调用的函数,该函数将打印调用者的__doc__属性并退出。 The function is the following: 该函数如下:

def checktype(objects,instances):
    if not all([isinstance(obj,instance) for
                obj,instance in zip(objects,instances)]):
      print 'Type Error'
      #Get __doc__ from caller
      print __doc__
      exit()

I got stuck in the step, where I have to get the __doc__ attribute. 我陷入了必须获得__doc__属性的步骤。 I know that inspect module can do it, in a way like the following: 我知道inspect模块可以通过以下方式做到这一点:

name=inspect.stack()[1][3]
possibles=globals().copy()
__doc__= possibles.get(name).__doc__

(you can suggest another one that is compatible with every Python version, including 3.5) (您可以建议另一个与每个Python版本(包括3.5版本)兼容的版本)

but I think there must be another way. 但我认为必须有另一种方式。 The reason for my scepticism is that the built-in return statement returns something to the caller in an immediate way, so that means there must be a "hook" or a "pipe" accessible by the child function, which is being used as a medium for information exchange with the parent.So an initial question that triggered my interest was: 我对此表示怀疑的原因是,内置的return语句会立即将某些内容返回给调用方,因此这意味着必须有一个可以由child函数访问的“ hook”或“ pipe”,它被用作与父母进行信息交流的媒介。因此,引起我兴趣的第一个问题是:

Is this pipe send-only and no information can be send backwards? 该管道是仅发送的,没有信息可以向后发送吗?

I have not been able to answer this, as the return statement is only briefly explained in the sites I searched. 我无法回答这个问题,因为return语句仅在我搜索的站点中得到了简要说明。 Apart from this, the inspect module, as far as I can tell, saves multiple frames in a stack and runs constantly in the background. 除此之外,据我所知, inspect模块将多个帧保存在堆栈中,并在后台不断运行。 For me, this is like I am trying to kill a fly with a minigun. 对我来说,这就像我试图用迷你枪杀死一只苍蝇。 I just need the caller function's name, not the function 10 frames before. 我只需要调用者函数的名称,而不是10帧之前的函数。 If there is not any way to accomplish this, this is, in my opinion, a feature that Python must have. 在我看来,如果没有任何方法可以实现,那么这就是Python必须具备的功能。 My question is: 我的问题是:

What would be the pythonic-programmatic way to get caller's attributes in Python, with universal support ? 在通用支持下,用Python编程的方式在Python中获取调用者的属性是什么? Excuse me if there is ignorance in my question, I am open to any corrections and "mind-openings". 如果我的问题中有无知,请原谅。我愿意接受任何更正和“开诚布公”。 Thank you all for your answers. 谢谢大家的答案。

I have a few functions that may be related to your issue 我有一些功能可能与您的问题有关

import sys

def position(level = 0):
    """return a tuple (code, lasti, lineno) where this function is called

    If level > 0, go back up to that level in the calling stack.
    """
    frame = sys._getframe(level + 1)
    try:
        return (frame.f_code, frame.f_lasti, frame.f_lineno)
    finally:
        del frame

def line(level = 0):
    """return a tuple (lineno, filename, funcname) where this function is called

    If level > 0, go back up to that level in the calling stack.

    The filename is the name in python's co_filename member
    of code objects.
    """
    code, lasti, lineno = position(level=level+1)
    return (lineno, code.co_filename, code.co_name)

def _globals(level = 0):
    """return the globals() where this function is called

    If level > 0, go back up to that level in the calling stack.

    """
    frame = sys._getframe(level + 1)
    try:
        return frame.f_globals
    finally:
        del frame

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM