Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #18257
| References | <00667d71-b9bc-411d-b2bc-0ead1d1468d7@g41g2000yqa.googlegroups.com> <mailman.4250.1325270163.27778.python-list@python.org> <a9e70268-ee24-4d18-abe2-e994e326be9c@d9g2000yqg.googlegroups.com> <mailman.4255.1325281766.27778.python-list@python.org> <4b0f3a04-3b29-4cb7-af52-2001b25d560a@t30g2000vbx.googlegroups.com> |
|---|---|
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | 2011-12-31 10:13 -0700 |
| Subject | Re: How to get function string name from i-th stack position? |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4264.1325351621.27778.python-list@python.org> (permalink) |
On Sat, Dec 31, 2011 at 1:44 AM, dmitrey <dmitrey15@gmail.com> wrote: > On Dec 30, 11:48 pm, Ian Kelly <ian.g.ke...@gmail.com> wrote: >> On Fri, Dec 30, 2011 at 11:43 AM, dmitrey <dmitre...@gmail.com> wrote: >> > Thank you. And what should I do to get function by itself instead of >> > its string name, e.g. I want to know does this function is my_func or >> > any other? For example, I would like to check is this function Python >> > sum(), or maybe numpy.sum(), or anything else? >> >> The Python stack only includes Python code objects. Built-ins like >> sum won't appear in it because they're basically C functions and don't >> have associated code objects. If you really want to see them, you >> could probably do something with ctypes to inspect the C stack, but I >> don't recommend it. >> >> You can get the Python code objects from the stack by calling >> inspect.stack(), which includes each frame object currently on the >> stack as the first member of each tuple. E.g.: >> >> frames = map(operator.itemgetter(0), inspect.stack()) >> >> Each frame has an f_code attribute that stores the code object >> associated with that frame. Getting the actual function from the code >> object is tricky, for two reasons. One, not all code objects >> represent functions. There are also code objects for modules, class >> definitions, and probably other thing as well. Two, code objects >> don't have associated functions. The relationship is the reverse: >> functions have associated code objects. You would have to iterate >> over each function that you're interested in, looking for one with a >> func_code attribute that "is" the frame's f_code attribute. >> >> In any case, testing function identity is a rather large rabbit hole >> that is best avoided. These are mathematically the same function: >> >> def plus1(value): >> return value + 1 >> >> plus_one = lambda x: x + 1 >> >> But they are two distinct function objects, and there is no way >> programmatically to determine that they are the same function except >> by comparing the bytecode (which won't work generally because of the >> halting problem). >> >> What is it that you're trying to do? Perhaps the helpful folks on the >> list will be able to suggest a better solution if you can provide more >> details. >> >> Cheers, >> Ian > > Maybe it is somehow possible to compare function id with my candidates > id, e.g. > PythonSumID = id(sum) > import numpy > NumpySumID = id(numpy.sum) > func = getting_function_from_Nth_stack_level_above > if id(func) == PythonSumID: > .... > elif id(func) == NumpySumID: > .... > else: > .... > I need it due to the following reason: FuncDesigner users often use > Python or numpy sum on FuncDesigner objects, while FuncDesigner.sum() > is optimized for this case, works faster and doesn't lead to "Max > recursion dept exceeded", that sometimes trigger for numpy or Python > sum() when number of summarized elements is more than several > hundreds. I would like to print warning "you'd better use FuncDesigner > sum" if this case has been identified. The only think I can think of would be to replace the sum built-in with a wrapper that checks whether it's being called on FuncDesigner objects and issues a warning. Users might not like you messing with their built-ins in that way, though.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How to get function string name from i-th stack position? dmitrey <dmitrey15@gmail.com> - 2011-12-30 09:51 -0800
Re: How to get function string name from i-th stack position? Tim Chase <python.list@tim.thechases.com> - 2011-12-30 12:35 -0600
Re: How to get function string name from i-th stack position? dmitrey <dmitrey15@gmail.com> - 2011-12-30 10:43 -0800
Re: How to get function string name from i-th stack position? Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-30 14:48 -0700
Re: How to get function string name from i-th stack position? dmitrey <dmitrey15@gmail.com> - 2011-12-31 00:44 -0800
Re: How to get function string name from i-th stack position? Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-31 10:13 -0700
Re: How to get function string name from i-th stack position? Lie Ryan <lie.1296@gmail.com> - 2011-12-31 22:41 +1100
Re: How to get function string name from i-th stack position? Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-31 10:05 -0700
csiph-web