introspection

Ruby String#to_class

三世轮回 提交于 2019-11-28 10:47:05
问题 Taken from a previous post with some modifications to respond to sepp2k's comment about namespaces, I have implemented String#to_class method. I'm sharing the code here and I do believe that it could be refactored someway specially the "i" counter. Your comments are appreciated. class String def to_class chain = self.split "::" i=0 res = chain.inject(Module) do |ans,obj| break if ans.nil? i+=1 klass = ans.const_get(obj) # Make sure the current obj is a valid class # Or it's a module but not

In Objective C, how to find out the return type of a method via reflection?

别说谁变了你拦得住时间么 提交于 2019-11-28 08:53:01
问题 @interface Foo : NSObject { } - (Bar)bar; At runtime, given [Foo class], how do we find out the return type of all methods Foo has? I have been studying the Objective-C runtime API for a while now; as far as I could tell, there is no way to get the return type (i.e. Class) of a method or a property. This seems to be a serious omission in any reflection API. 回答1: You can tell basic information like what C type it is, but you can't differentiate between various instance types (or classes) on

Implementing NSCopying in Swift with subclasses

天大地大妈咪最大 提交于 2019-11-28 07:41:21
Consider two classes. The first is Vehicle , an NSObject subclass that conforms to NSCopying : class Vehicle : NSObject, NSCopying { var wheels = 4 func copyWithZone(zone: NSZone) -> AnyObject { let vehicle = self.dynamicType() vehicle.wheels = self.wheels return vehicle } } The second class, Starship , inherits from Vehicle : class Starship : Vehicle { var photonTorpedos = 6 var antiGravity = true override func copyWithZone(zone: NSZone) -> AnyObject { let starship = super.copyWithZone(zone) as Starship starship.photonTorpedos = self.photonTorpedos starship.antiGravity = self.antiGravity

How to check if an object is an instance of a namedtuple?

柔情痞子 提交于 2019-11-28 07:13:25
How do I check if an object is an instance of a Named tuple ? Alex Martelli Calling the function collections.namedtuple gives you a new type that's a subclass of tuple (and no other classes) with a member named _fields that's a tuple whose items are all strings. So you could check for each and every one of these things: def isnamedtupleinstance(x): t = type(x) b = t.__bases__ if len(b) != 1 or b[0] != tuple: return False f = getattr(t, '_fields', None) if not isinstance(f, tuple): return False return all(type(n)==str for n in f) it IS possible to get a false positive from this, but only if

Get PHP class namespace dynamically

不羁的心 提交于 2019-11-28 07:09:17
问题 How can I retrieve a class namespace automatically? The magic var __NAMESPACE__ is unreliable since in subclasses it's not correctly defined. Example: class Foo\bar\A -> __NAMESPACE__ === Foo\bar class Ping\pong\B extends Foo\bar\A -> __NAMESPACE__ === Foo\bar (it should be Ping\pong) ps: I noticed the same wrong behavior using __CLASS__ , but I solved using get_called_class() ... is there something like get_called_class_namespace() ? How can I implement such function? UPDATE: I think the

What is the correct way to override the __dir__ method?

青春壹個敷衍的年華 提交于 2019-11-28 06:57:03
问题 This question is meant to be more about __dir__ than about numpy . I have a subclass of numpy.recarray (in python 2.7, numpy 1.6.2), and I noticed recarray 's field names are not listed when dir ing the object (and therefore ipython's autocomplete doesn't work). Trying to fix it, I tried overriding __dir__ in my subclass, like this: def __dir__(self): return sorted(set( super(MyRecArray, self).__dir__() + \ self.__dict__.keys() + self.dtype.fields.keys())) which resulted with: AttributeError:

How do I get the filepath for a class in Python?

吃可爱长大的小学妹 提交于 2019-11-28 04:48:23
Given a class C in Python, how can I determine which file the class was defined in? I need something that can work from either the class C, or from an instance off C. The reason I am doing this, is because I am generally a fan off putting files that belong together in the same folder. I want to create a class that uses a Django template to render itself as HTML. The base implementation should infer the filename for the template based on the filename that the class is defined in. Say I put a class LocationArtifact in the file "base/artifacts.py", then I want the default behaviour to be that the

Python inspect.stack is slow

霸气de小男生 提交于 2019-11-28 04:01:39
问题 I was just profiling my Python program to see why it seemed to be rather slow. I discovered that the majority of its running time was spent in the inspect.stack() method (for outputting debug messages with modules and line numbers), at 0.005 seconds per call. This seems rather high; is inspect.stack really this slow, or could something be wrong with my program? 回答1: inspect.stack() does two things: collect the stack by asking the interpreter for the stack frame from the caller ( sys._getframe

Get all methods of an Objective-C class or instance

大兔子大兔子 提交于 2019-11-28 03:34:33
In Objective-C I can test whether a given class or instance responds to certain selectors. But how can query a class or instance for all its methods or properties of a class (e.g. a list of all methods or properties)? Ben Gottlieb You'll want to use the Objective C runtime methods, see here: https://developer.apple.com/reference/objectivec/objective_c_runtime You can do this and it is extremely well documented at https://developer.apple.com/library/mac/documentation/cocoa/Reference/ObjCRuntimeRef/index.html To fetch all the instance or class methods of a class, you may use class_copyMethodList

How to introspect django model fields?

半腔热情 提交于 2019-11-28 03:34:33
I am trying to obtain class information on a field inside a model, when I only know name of the field and name of the model (both plain strings). How is it possible? I can load the model dynamically: from django.db import models model = models.get_model('myapp','mymodel') Now I have field - 'myfield' - how can I get the class of that field? If the field is relational - how to get related field? Thanks a bunch! Anurag Uniyal You can use model's _meta attribute to get field object and from field you can get relationship and much more e.g. consider a employee table which has a foreign key to a