-6

I've poked around at the PyObjC innards quite a bit trying to figure this out. Is it possible to access Objective-C's hidden SEL _cmd method argument when writing a Python method? It's got to be generated at some point, but I'm not sure if that point is one at which my Python code can get to it.

I was mostly interested in this in order to be able to make an easy PyObjC NSLog text macro:

def meth_(self, arg):
    NSLog(u"%s called" % _cmd)

although I have found other ways to do this kind of logging in Python (see Jeremy's answer), so at this point it's become curiosity about the PyObjC bridge.

0

2 Answers 2

3

Why not go about it by logging the name of the Python function, instead? Then you can apply a Python approach to getting the function name, like throwing an exception and munging around in the stack trace, as shown in this code snippet.

ETA: This code demonstrates the "better way" mentioned in the prior snippet as introduced in Python 2.1: using sys._getframe(). It is far simpler.

Sign up to request clarification or add additional context in comments.

1 Comment

Indeed. I've seen these and made use of the second when necessary. Not exactly what I'm looking for, but I appreciate the answer.
-1

I am not familiar with Python. But in objective-C instead _cmd you can find method selector by:

const id* selfPtr = &self;
SEL sel = *(SEL*)(void*)(--selfPtr);

May be you can use same approach in Python.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.