introspection

How do I get the string representation of a variable in python?

元气小坏坏 提交于 2019-12-22 06:10:54
问题 I have a variable x in python. How can i find the string 'x' from the variable. Here is my attempt: def var(v,c): for key in c.keys(): if c[key] == v: return key def f(): x = '321' print 'Local var %s = %s'%(var(x,locals()),x) x = '123' print 'Global var %s = %s'%(var(x,locals()),x) f() The results are: Global var x = 123 Local var x = 321 The above recipe seems a bit un-pythonesque. Is there a better/shorter way to achieve the same result? 回答1: Q: I have a variable x in python. How can i

Spy++ for PowerBuilder applications

三世轮回 提交于 2019-12-22 05:43:16
问题 I'm trying to write a tool which lets me inspect the state of a PowerBuilder-based application. What I'm thinking of is something like Spy++ (or, even nicer, 'Snoop' as it exists for .NET applications) which lets me inspect the object tree (and properties of objects) of some PowerBuilder-based GUI. I did the same for ordinary (MFC-based) applications as well as .NET applications already, but unfortunately I never developed an application in PowerBuilder myself, so I'm generally thinking about

Find out if an Objective-C class overrides a method [duplicate]

可紊 提交于 2019-12-22 04:47:07
问题 This question already has answers here : Objective-C detect if class overrides inherited method (2 answers) Closed 4 years ago . How can I find out, at runtime, if a class overrides a method of its superclass? For example, I want to find out if a class has it's own implementation of isEqual: or hash , instead of relying on a super class. 回答1: You just need to get a list of the methods, and look for the one you want: #import <objc/runtime.h> BOOL hasMethod(Class cls, SEL sel) { unsigned int

Get kwargs Inside Function

纵然是瞬间 提交于 2019-12-22 01:37:36
问题 If I have a python function like so: def some_func(arg1, arg2, arg3=1, arg4=2): Is there a way to determine which arguments were passed by keyword from inside the function? EDIT For those asking why I need this, I have no real reason, it came up in a conversation and curiosity got the better of me. 回答1: No, there is no way to do it in Python code with this signature -- if you need this information, you need to change the function's signature. If you look at the Python C API, you'll see that

Object Reflection

ε祈祈猫儿з 提交于 2019-12-21 22:09:08
问题 Does anyone have any references for building a full Object/Class reflection system in C++ ? Ive seen some crazy macro / template solutions however ive never found a system which solves everything to a level im comfortable with. Thanks! 回答1: Using templates and macros to automatically, or semi-automatically, define everything is pretty much the only option in C++. C++ has very weak reflection/introspection abilities. However, if what you want to do is mainly serialization and storage, this has

Introspection and iteration on an Enum

送分小仙女□ 提交于 2019-12-21 17:32:40
问题 Is it possible to programatically find out how many "cases" an Enum has in Swift 2 and iterate over them? This code doesn't compile, but it gives yo an idea of what I'm trying to achieve: enum HeaderStyles{ case h1 case h2 case h3 } for item in HeaderStyles{ print(item) } 回答1: You can write a generic struct that provide that iteration ability. In example below the enum raw values must start with 0 (it does it by default) and have no gaps between raw values (can't have raw values such as 0,1,2

Change reference to function in run-time in Python

大兔子大兔子 提交于 2019-12-21 17:24:50
问题 I need to change a call to a function inside another function during run-time. Consider the following code: def now(): print "Hello World!" class Sim: def __init__(self, arg, msg): self.msg = msg self.func = arg self.patch(self.func) def now(self): print self.msg def run(self): self.func() def patch(self, func): # Any references to the global now() in func # are replaced with the self.now() method. def myfunc(): now() Then ... >>> a = Sim(myfunc, "Hello Locals #1") >>> b = Sim(myfunc, "Hello

How to generate GIR files from the Vala compiler?

瘦欲@ 提交于 2019-12-21 04:27:19
问题 I am trying to create python bindings to a vala library using pygi with gobject introspection. However, I am having trouble generating the GIR files (that I am planning to compile to typelib files subsequently). According to the documentation valac should support generating GIR files. Compiling the following helloworld.vala public struct Point { public double x; public double y; } public class Person { public int age = 32; public Person(int age) { this.age = age; } } public int main() { var p

instantiate from protocol.Type reference dynamically at runtime

拥有回忆 提交于 2019-12-20 02:11:44
问题 I've asked this question earlier at so you get to know a bit of history, It was an awesome attempt by Airspeed Velocity there but I feel I didn't quite get there yet, so I'm narrowing down my question to very minute details in order to really crack it down. swift program to interface You may complain or down vote that question is incomplete but that's how it goes, It's based on design patterns, so if you're not familiar with design patterns or philosophy "Program to interface not to an

How to introspect all available methods and members of a Rust type?

放肆的年华 提交于 2019-12-19 12:52:02
问题 Is there a way to print out a complete list of available members of a type or instance in Rust? For example: In Python, I can use print(dir(object)) In C, Clang has a Python API that can parse C code and introspect it. Being unfamiliar with Rust tools, I'm interested to know if there is some way to do this, either at run-time or compile-time, either with compiler features (macros for example), or using external tools. This question is intentionally broad because the exact method isn't