introspection

In Python, determine if a function calls another function

佐手、 提交于 2019-12-11 12:46:53
问题 How can I determine programmatically if one function calls another function? I cannot modify either function. Here's what I want ( source_calls_target ): >>> def check(): >>> pass >>> def test_check(): >>> check() >>> def source_calls_target(source, target): >>> # if source() calls target() somewhere, return True >>> ??? >>> source_calls_target(test_check, check) True >>> source_calls_target(check, test_check) False Ideally, I do not want to actually call target() . Ideally, I want to check

Introspect Scala traits

北战南征 提交于 2019-12-11 10:57:04
问题 Consider the example class FooListener extends Listener { @Listen def runMeToo = { ... } } trait Listener { @Listen def runMe = { ... } } I'm writing introspection code to find all methods of a given class (ie FooListener ) annotated with a certain annotation (ie @Listen ). They'll be invoked under certain circumstances. So I need all their java.lang.Method instances. It's easy to find those methods in the FooListener class. Easy also to find those of the super classes. The question is how to

iPad. UIBarButtonItem has an undocumented view of type UIToolbarTextButton. Huh?

限于喜欢 提交于 2019-12-11 06:56:22
问题 I have an iPad app where I have a view controller that is the UIGestureRecognizerDelegate for a number of UIGestureRecognizers. I have implemented the following method of UIGestureRecognizerDelegate: - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { // Double tapping anywhere on the screen hides/shows the toolbar if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]] == YES) { if (touch.tapCount == 2) { self.toolbar.hidden =

Which programming language has the best introspection and reflection capabilities? [closed]

岁酱吖の 提交于 2019-12-11 06:28:31
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 3 years ago . Many languages have the ability for introspection and reflection. However, some are clearly better than others. Any suggestions as to which languages are "better" in this respect, and why. 回答1: Reflection is essentially the ability to extract properties of the source program.

Why use getattr() instead of __dict__ when accessing Python object attributes?

烈酒焚心 提交于 2019-12-11 04:07:06
问题 In source examples and SO answers that have some degree of Python object introspection, a common pattern is: getattr(some_object, attribute_name_string) Is there a reason why this pattern is preferred to: some_object.__dict__[attribute_name_string] which seems to be more directly showing what's going on? Is it because the latter is too close to a particular implementation in CPython that may be subject to change? NB Original question incorrectly identified the popular idiom as: some_object._

inspect who imported me

亡梦爱人 提交于 2019-12-11 03:54:47
问题 There are two files: # the_imported.py import inspect imported_by_fname = inspect.currentframe().f_back.f_code.co_filename print('{} was imported by {}'.format(__name__, imported_by_fname)) And: # the_importer.py import the_imported When executed with Python 2.7: $ python the_importer.py the_imported was imported by the_importer.py When executed with Python 3.5: $ python3 the_importer.py the_imported was imported by <frozen importlib._bootstrap> What is that weird thing <frozen importlib.

Python - How can I query the class in which a method is defined?

[亡魂溺海] 提交于 2019-12-11 03:06:06
问题 My question is somewhat similar to this one; it concerns object methods rather than module contents. I want to know if I can use the inspect module to get the methods defined only in the class I'm asking about , and not its parent(s). I need this because my child class defines 'macro' methods, which accesses the parent's methods at a higher level of abstraction, and I don't want the user to have to worry about the lower-level methods defined all the way up the inheritance tree. Here is a

C++ - Good or bad practice?

狂风中的少年 提交于 2019-12-11 02:47:30
问题 I'm facing situations where i'm finding it handy to store the type (as an enum) of an object in the base class to further cast a pointer to that base class into a subclass pointer depending on the value of that type. For example : class CToken { public: token_type_e eType; }; class COperatorToken : public CToken { public: // Sub class specific stuff }; class CLiteralToken : public CToken { public: // Sub class specific stuff }; And then vector<CToken *> aTokens; //... for( size_t nI = 0,

What happens to a block at compile time, and can I create one at runtime?

烈酒焚心 提交于 2019-12-11 02:36:12
问题 This is a two-part question about blocks (^{}) in Objective-C . I have searched for some answers, but nothing has showed up in Google or SO. This question stems from a desire to create an custom XML Layout Engine for iOS, with support for blocks - meaning that I want to parse NSStrings and create a block during runtime. 1) Is this even possible? If so, how can it be done? Not being able to find much on NSString to Block , I thought that the reason may be how a block is handled by the compiler

Detecting bound method in classes (not instances) in Python 3

感情迁移 提交于 2019-12-11 02:02:05
问题 Given a class C with a function or method f , I use inspect.ismethod(obj.f) (where obj is an instance of C ) to find out if f is bound method or not. Is there a way to do the same directly at the class level (without creating an object)? inspect.ismethod does not work as this: class C(object): @staticmethod def st(x): pass def me(self): pass obj = C() results in this (in Python 3): >>> inspect.ismethod(C.st) False >>> inspect.ismethod(C.me) False >>> inspect.ismethod(obj.st) False >>> inspect