introspection

How to get the arity of a method?

寵の児 提交于 2019-12-13 18:20:01
问题 In Javascript, for example, you can get the arity (the number of arguments a function is supposed to take) by simply func.length . How can I get this information for a method in Objective-C? 回答1: NSMethodSignature is awesome for all kind of reflection information. SEL selector = @selector(foo:); NSMethodSignature *sig = [someObj methodSignatureForSelector:selector] int argCount = [sig numberOfArguments]; 回答2: When there’s no receiver object to send -methodSignatureForSelector: to, it’s

Get function name as a string in python [duplicate]

余生长醉 提交于 2019-12-13 13:15:24
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How do I get the name of a function or method from within a Python function or method? How to get the function name as string in Python? I have a function named func , I'd like to be able to get the functions name as a string. pseudo-python : def func () : pass print name(func) This would print 'func'. 回答1: That's simple. print func.__name__ EDIT: But you must be careful: >>> def func(): ... pass ... >>> new

creating Python classes with arbitrarily substituted attribute name

亡梦爱人 提交于 2019-12-13 04:09:18
问题 I apologize for not giving this question a better title; the reason that I am posting it is that I don't even have the correct terminology to know what I am looking for. I have defined a class with an attribute 'spam': def SpamClass(object): def __init__(self, arg): self.spam = arg def __str__(self): return self.spam I want to create a (sub/sibling?)class that has exactly the same functionality, but with an attribute named 'eggs' instead of 'spam': def EggsClass(object): def __init__(self,

JavaBeans Introspector does not correctly find property with interface hierarchy type

本秂侑毒 提交于 2019-12-13 02:11:41
问题 I read the JavaBeans specs but I found nowhere this behavior. Is it a bug ? testPropertyType fails because expects Data class testPropertyReadable succeed because DefaultBean.getMyData returning Data method exists testPropertyWritable fails because no DefaultBean.setMyData(Data) method does not exists Tested on JavaSE 6 import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; import org.junit.Test; public class DefaultBeanTest { @Test public void

How to load an arbitrary java .class file from the filesystem and reflect on it?

南楼画角 提交于 2019-12-12 21:04:32
问题 I want to make a command-line utility that does some operations based on reflection of external class files. I would pass in a path to .class files or source files(possibly wildcards). At some point during the execution, I need to get Class objects for each class, not knowing their package names beforehand. What would it take to do this? What can I look at to get started? I also have access to the classes' source files. this is in java 1.6. also, would it be easier to get class objects from

Is it possible to write to a python frame object as returned by sys._getframe() from python code running within the interpreter?

 ̄綄美尐妖づ 提交于 2019-12-12 08:18:39
问题 Apropos of This question, there is a bit of scaffolding within the interpreter to inspect frame objects, which can be retrieved by sys._getframe() . The frame objects appear to be read only, but I can't find anything obvious in the docs that explicitly states this. Can someone confirm whether these objects are writeable (in some way) or read only? import sys def foobar(): xx='foo' ff = sys._getframe() ff.f_locals['xx'] = 'bar' print xx if __name__ == '__main__': foobar() This prints out ' foo

How does the “#map(&proc)” idiom work when introspecting module classes?

丶灬走出姿态 提交于 2019-12-12 07:45:19
问题 Presenting the Idiom I found an interesting but unexplained alternative to an accepted answer. The code clearly works in the REPL. For example: module Foo class Bar def baz end end end Foo.constants.map(&Foo.method(:const_get)).grep(Class) => [Foo::Bar] However, I don't fully understand the idiom in use here. In particular, I don't understand the use of &Foo , which seems to be some sort of closure, or how this specific invocation of #grep operates on the result. Parsing the Idiom So far, I

Accessing original decorated function for test purposes

只愿长相守 提交于 2019-12-12 07:22:33
问题 I'm using a decorator( @render_to from the django_annoying package) in a view function. But the thing is that I wanted to get the original dict that is returned by the view function for test purposes, instead of the HttpResponse object that the decorator returns. The decorator uses @wraps (from functools ). If there is no way to access this, then do you have any idea of how to test this? 回答1: The wrapped function will be available as a function closure cell. Which cell exactly depends on how

Can I inspect the proptypes of react component class if given an instance?

久未见 提交于 2019-12-12 00:29:17
问题 I'd love to access the propTypes of a class, e.g. var Button = React.createClass({ propTypes: { text: React.PropTypes.string, href: React.PropTypes.string }, ... }) ... var ButtonPropTypes = Button.propTypes I'd expect to see something along the lines of { text: function(){...}, href: function(){...} } But instead I get undefined . What am I doing wrong, and how can I get at the proptypes? 回答1: You should be able to access the proptypes statically as you are doing in your code. I put an

bash caller builtin stops working from exported function, why?

梦想与她 提交于 2019-12-11 12:57:20
问题 I have very strange issues using bash and exported function to give me a reliable answer to the call of the builtin caller . Here's my setup to illustrate this issue: Bash script bar defines and exports function bar1 and bar2 . bar2 calls bar1 . Bash script bar then execute bash script foo which will call bar1 . The caller builtin will then break only after the call of bar1 . Is this normal ? can you explain why ? can you simplify the following code that exposes the issue ? To be perfectly