introspection

Python3: check if method is static

人走茶凉 提交于 2019-12-19 07:27:10
问题 Simmilar question (related with Python2: Python: check if method is static) Lets concider following class definition: class A: def f(self): return 'this is f' @staticmethod def g(): return 'this is g' In Python 3 there is no instancemethod anymore, everything is function, so the answer related to Python 2 will not work anymore. As I told, everything is function, so we can call A.f(0) , but of course we cannot call A.f() (argument missmatch). But if we make an instance a=A() and we call a.f()

Viewing the code of a Python function [duplicate]

安稳与你 提交于 2019-12-19 05:15:02
问题 This question already has answers here : How can I get the source code of a Python function? (13 answers) Closed 5 years ago . Let's say I'm working in the Python shell and I'm given a function f . How can I access the string containing its source code? (From the shell, not by manually opening the code file.) I want this to work even for lambda functions defined inside other functions. 回答1: inspect.getsource It looks getsource can't get lambda's source code. 回答2: Not necessarily what you're

Ruby method interception

为君一笑 提交于 2019-12-18 17:01:06
问题 I want to intercept method calls on a ruby-class and being able to do something before and after the actual execution of the method. I tried the following code, but get the error: MethodInterception.rb:16:in before_filter': (eval):2:in alias_method': undefined method say_hello' for class HomeWork' (NameError) from (eval):2:in `before_filter' Can anybody help me to do it right? class MethodInterception def self.before_filter(method) puts "before filter called" method = method.to_s eval_string

Get function callers' information in python

牧云@^-^@ 提交于 2019-12-18 15:51:35
问题 I want to get information about the callers of a specific function in python. For example: class SomeClass(): def __init__(self, x): self.x = x def caller(self): return special_func(self.x) def special_func(x): print "My caller is the 'caller' function in an 'SomeClass' class." Is it possible with python? 回答1: Yes, the sys._getframe() function let's you retrieve frames from the current execution stack, which you can then inspect with the methods and documentation found in the inspect module;

Thread name longer than 15 chars?

送分小仙女□ 提交于 2019-12-18 14:49:22
问题 By using functions like prctl, or pthread_set_name_np it's possible to change the name of a thread. The limit both functions imposes, at least in Linux 2.6.38, is that the name cannot be longer than 15 characters (NULL termination being the 16th byte). Where is this 15 character limit imposed, and is there any (even unorthodox) way around it? Update : As mentioned in the comments, this is imposed by the kernel. The definition can be found here: http://lxr.linux.no/linux+v2.6.37/include/linux

Sorting the [Any] array

吃可爱长大的小学妹 提交于 2019-12-18 11:16:32
问题 Given an array defined as follow let list: [Any] I want to sort it WHEN all the values inside it have the same type Element AND Element is Comparable . When it should return the sorted array So I would need a function that when the array is populated in a way like the followings let list: [Any] = [10, 11, 0, 2, -1] let list: [Any] = ["Red", "Green", "Blue"] let list: [Any] = [true, false, true, true] does return the sorted array. When it should return nil On the other hand when list contains

Obj-C introspection: How can a method reference its own selector?

僤鯓⒐⒋嵵緔 提交于 2019-12-18 08:58:56
问题 I wish to write a macro, to be used within any method, which references the method's selector. I do not wish to pass the literal name of the method. For example: #define RERUN [self performSelector:{something} withObject:nil afterDelay: 0.0] where the "{something}" in the above would resolve to the selector of whatever method the macro was used in. Is there some way to do this? 回答1: _cmd represents the selector of the current method -- it is a hidden argument (like self ). if you never need

Get source code of user-defined class and functions?

六月ゝ 毕业季﹏ 提交于 2019-12-18 06:18:05
问题 Is there a method for getting the source code of a class or method? I'm looking at the ReflectionClass, but I don't see one. 回答1: Best I could come up with: $class = new ReflectionClass($c); $fileName = $class->getFileName(); $startLine = $class->getStartLine()-1; // getStartLine() seems to start after the {, we want to include the signature $endLine = $class->getEndLine(); $numLines = $endLine - $startLine; if(!empty($fileName)) { $fileContents = file_get_contents($fileName); $classSource =

Accessing a method in a super class when it's not exposed

老子叫甜甜 提交于 2019-12-18 03:56:53
问题 In a subclass, I'm overriding a method that is not exposed in the super class. I know that I have the correct signature as it is successfully overriding the superclass implementation. However, as part of the the new implementation, I need to call the superclass's implementation from the subclass's implementation. Because it's not exposed I have to invoke the method via a call to performSelector : SEL superClassSelector = NSSelectorFromString(@"methodToInvoke"); [super performSelector

Call private methods and private properties from outside a class in PHP

China☆狼群 提交于 2019-12-18 03:09:13
问题 I want to access private methods and variables from outside the classes in very rare specific cases. I've seen that this is not be possible although introspection is used. The specific case is the next one: I would like to have something like this: class Console { final public static function run() { while (TRUE != FALSE) { echo "\n> "; $command = trim(fgets(STDIN)); switch ($command) { case 'exit': case 'q': case 'quit': echo "OK+\n"; return; default: ob_start(); eval($command); $out = ob