introspection

How to access properties of Python super classes e.g. via __class__.__dict__?

戏子无情 提交于 2019-11-28 03:23:03
问题 How can I get all property names of a python class including those properties inherited from super classes ? class A(object): def getX(self): return "X" x = property(getX) a = A() a.x 'X' class B(A): y = 10 b = B() b.x 'X' a.__class__.__dict__.items() [('__module__', '__main__'), ('getX', <function getX at 0xf05500>), ('__dict__', <attribute '__dict__' of 'A' objects>), ('x', <property object at 0x114bba8>), ('__weakref__', <attribute '__weakref__' of 'A' objects>), ('__doc__', None)] b._

How do I look inside a Python object?

拈花ヽ惹草 提交于 2019-11-28 02:31:11
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? Brandon E Taylor Python has a strong set of introspection features. Take a look at the following built-in functions : type() dir() id() getattr() hasattr() globals() locals() callable() type() and dir() are particularly useful

Get the name (string) of a generic type in Swift

半世苍凉 提交于 2019-11-27 23:02:06
问题 I have a generic class of type T and I would like to get the name of the type that passed into the class when instantiated. Here is an example. class MyClass<T> { func genericName() -> String { // Return the name of T. } } I have been looking around for hours and I can't seem to find any way to do this. Has anyone tried this yet? Any help is greatly appreciated. Thanks 回答1: A pure swift way to achieve that is not possible. A possible workaround is: class MyClass<T: AnyObject> { func

How to introspect a function defined in a Cython C extension module

倖福魔咒の 提交于 2019-11-27 21:58:36
问题 Python's inspect module doesn't seem to be able to inspect the signatures of "built-in" functions, which include functions defined in C extension modules, like those defined by Cython. Is there any way to get the signature of a Python function you have defined in such a module, and specifically in Cython? I am looking to be able to find the available keyword arguments. MWE: # mwe.pyx def example(a, b=None): pass and import pyximport; pyximport.install() import mwe import inspect inspect

How Do I Perform Introspection on an Object in Python 2.x?

痴心易碎 提交于 2019-11-27 21:55:54
I'm using Python 2.x and I have an object I'm summoning from the aether; the documentation on it is not particularly clear. I would like to be able to get a list of properties for that object and the type of each property. Similarly, I'd like to get a list of methods for that object, as well, plus any other information I could find on that method, such as number of arguments and their respective types. I have a feeling that I am simply missing the correct jargon in my Google searches. Not that I want to derail with specifics, but it's Active Directory, so that's always fun. Well ... Your first

Generate SQL to update primary key

这一生的挚爱 提交于 2019-11-27 21:40:29
问题 I want to change a primary key and all table rows which reference to this value. # table master master_id|name =============== foo|bar # table detail detail_id|master_id|name ======================== 1234|foo|blu If I give a script or function table=master, value-old=foo, value-new=abc I want to create a SQL snippet that executes updates on all tables which refere to table "master": update detail set master_id=value-new where master_id=value-new; ..... With the help of introspection, this

Watch for a variable change in python

孤人 提交于 2019-11-27 19:29:19
There is large python project where one attribute of one class just have wrong value in some place. It should be sqlalchemy.orm.attributes.InstrumentedAttribute, but when I run tests it is constant value, let's say string. There is some way to run python program in debug mode, and run some check (if variable changed type) after each step throught line of code automatically? P.S. I know how to log changes of attribute of class instance with help of inspect and property decorator. Possibly here I can use this method with metaclasses... But sometimes I need more general and powerfull solution...

Python: How to retrieve class information from a 'frame' object?

谁说我不能喝 提交于 2019-11-27 19:28:41
Is it possible to retrieve any class information from a frame object? I know how to get the file (frame.f_code.co_filename), function (frame.f_code.co_name) and line number (frame.f_lineno), but would like to be able to also get the name of the class of the active object instance of the frame (or None if not in an instance). I don't believe that, at the frame object level, there's any way to find the actual python function object that has been called. However, if your code rely on the common convention : naming the instance parameter of a method self , then you could do the following : def get

Get Activity name dynamically - android

谁都会走 提交于 2019-11-27 18:26:50
I'd like to get the name of the current Activity to be sent along in the URI of an HttpRequest . Is there a way to do this without referring specifically to the Activity ? I know I can do myActivity.class.toString() but this is just a less efficient way of hard coding "myActivity" since I'm making a static reference to my Activity . Is there a more general way to do this using something like 'this' (which btw doesn't actually work here because it returns more information than what's desired). Use this.getClass().getSimpleName() to get the name of the Activity. From the comments, if you're in

Can one access the template parameter outside of a template without a typedef?

浪子不回头ぞ 提交于 2019-11-27 18:08:48
问题 A simple example: template<typename _X> // this template parameter should be usable outside! struct Small { typedef _X X; // this is tedious! X foo; }; template<typename SomeSmall> struct Big { typedef typename SomeSmall::X X; // want to use X here! SomeSmall bar; X toe; }; Is there a way to access the template parameter X of Small without using a typedef in the Small class? 回答1: Depending on what you're doing, template template parameters might be a better option: // "typename X" is a