introspection

Why is important to include “.moc” file at end of a Qt Source code file?

怎甘沉沦 提交于 2019-11-27 02:09:50
Why is it important to add an include for .moc file in a Qt cpp source code? This is a common step used in several Qt samples, including this one: http://doc.qt.io/qt-5/qttestlib-tutorial1-example.html ; where the line #include "testqstring.moc" should be included in the end of the file. I don't understand exactly why this is necessary. Thanks. It's necessary if you define QObject subclasses with the Q_OBJECT macro in a .cpp file. When you do so: qmake must generate rules inside your Makefile to invoke moc on that .cpp file. That special (hackish?) inclusion triggers qmake to do so, and tells

Implementing NSCopying in Swift with subclasses

南楼画角 提交于 2019-11-27 01:54:51
问题 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)

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

試著忘記壹切 提交于 2019-11-27 01:34:36
问题 How do I check if an object is an instance of a Named tuple? 回答1: 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

property type or class using reflection

这一生的挚爱 提交于 2019-11-27 01:29:29
I was wondering if it's possible to determine the class or primitive type of an Objects properties. Getting all properties names and values is pretty easy. SO answer So is there any way to get the properties class type while the property hast no value or nil value? Example Code @interface MyObject : NSObject @property (nonatomic, copy) NSString *aString; @property (nonatomic, copy) NSDate *aDate; @property NSInteger aPrimitive; @end @implementation MyObject @synthesize aString; @synthesize aDate; @synthesize aPrimitive; - (void)getTheTypesOfMyProperties { unsigned int count; objc_property_t*

Print all local variables accessible to the current scope in Lua

旧街凉风 提交于 2019-11-27 00:45:53
I know how to print "all" global variables using the following code for k,v in pairs(_G) do print("Global key", k, "value", v) end So my question is how to do that for all variables that are accessible from the currently executing function, something that can do what locals() does for Python. Here is an implementation of a locals() function. It will return a table of locals from the calling scope: function locals() local variables = {} local idx = 1 while true do local ln, lv = debug.getlocal(2, idx) if ln ~= nil then variables[ln] = lv else break end idx = 1 + idx end return variables end

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

冷暖自知 提交于 2019-11-27 00:32:25
问题 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

How to introspect django model fields?

a 夏天 提交于 2019-11-27 00:06:51
问题 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! 回答1: You can use model's _meta attribute to get field object and from field you can

Get all methods of an Objective-C class or instance

ぐ巨炮叔叔 提交于 2019-11-27 00:02:08
问题 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)? 回答1: You'll want to use the Objective C runtime methods, see here: https://developer.apple.com/reference/objectivec/objective_c_runtime 回答2: You can do this and it is extremely well documented at https://developer.apple.com/library/mac/documentation/cocoa/Reference

How do I look inside a Python object?

两盒软妹~` 提交于 2019-11-26 23:45:31
问题 I'm starting to code in various projects using Python (including Django web development and Panda3D game development). To help me understand what's going on, I would like to basically 'look' inside the Python objects to see how they tick - like their methods and properties. So say I have a Python object, what would I need to print out its contents? Is that even possible? 回答1: Python has a strong set of introspection features. Take a look at the following built-in functions: type() dir() id()

List all base classes in a hierarchy of given class?

前提是你 提交于 2019-11-26 23:33:44
Given a class Foo (whether it is a new-style class or not), how do you generate all the base classes - anywhere in the inheritance hierarchy - it issubclass of? Jochen Ritzel inspect.getmro(cls) works for both new and old style classes and returns the same as NewClass.mro() : a list of the class and all its ancestor classes, in the order used for method resolution. >>> class A(object): >>> pass >>> >>> class B(A): >>> pass >>> >>> import inspect >>> inspect.getmro(B) (<class '__main__.B'>, <class '__main__.A'>, <type 'object'>) See the __bases__ property available on a python class , which