introspection

How to get name of function's actual parameters in Python?

余生颓废 提交于 2019-12-02 09:08:07
问题 For example: def func(a): # how to get the name "x" x = 1 func(x) If I use inspect module I can get the stack frame object: import inspect def func(a): print inspect.stack() out: [ (<frame object at 0x7fb973c7a988>, 'ts.py', 9, 'func', [' stack = inspect.stack()\n'], 0) (<frame object at 0x7fb973d74c20>, 'ts.py', 18, '<module>', ['func(x)\n'], 0) ] or use inspect.currentframe() I can get the current frame. But I can only get the name "a" by inspect the function object. Use inspect.stack I can

Python: Why can't I modify the current scope within a function using locals()?

假装没事ソ 提交于 2019-12-02 04:35:24
问题 Why does creating/modifying a member of locals() not work within a function? Python 2.5 (release25-maint, Jul 20 2008, 20:47:25) [GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> # Here's an example of what I expect to be possible in a function: >>> a = 1 >>> locals()["a"] = 2 >>> print a 2 >>> # ...and here's what actually happens: >>> def foo(): ... b = 3 ... locals()["b"] = 4 ... print b ... >>> foo() 3

Python: Why can't I modify the current scope within a function using locals()?

China☆狼群 提交于 2019-12-01 23:24:03
Why does creating/modifying a member of locals() not work within a function? Python 2.5 (release25-maint, Jul 20 2008, 20:47:25) [GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> # Here's an example of what I expect to be possible in a function: >>> a = 1 >>> locals()["a"] = 2 >>> print a 2 >>> # ...and here's what actually happens: >>> def foo(): ... b = 3 ... locals()["b"] = 4 ... print b ... >>> foo() 3 Why would it? It's designed to return a representation, and was never intended for editing the locals.

Looping over a Python / IronPython Object Methods

こ雲淡風輕ζ 提交于 2019-12-01 21:28:35
问题 What is the proper way to loop over a Python object's methods and call them? Given the object: class SomeTest(): def something1(self): print "something 1" def something2(self): print "something 2" 回答1: You can use the inspect module to get class (or instance) members: >>> class C(object): ... a = 'blah' ... def b(self): ... pass ... ... >>> c = C() >>> inspect.getmembers(c, inspect.ismethod) [('b', <bound method C.b of <__main__.C object at 0x100498250>>)] getmembers() returns a list of

instantiate from protocol.Type reference dynamically at runtime

倾然丶 夕夏残阳落幕 提交于 2019-12-01 20:48:53
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 implementation" then don't complaint or down vote. Looking out for SWIFT guru's who can crack it down. All

Looping over a Python / IronPython Object Methods

﹥>﹥吖頭↗ 提交于 2019-12-01 19:38:49
What is the proper way to loop over a Python object's methods and call them? Given the object: class SomeTest(): def something1(self): print "something 1" def something2(self): print "something 2" You can use the inspect module to get class (or instance) members: >>> class C(object): ... a = 'blah' ... def b(self): ... pass ... ... >>> c = C() >>> inspect.getmembers(c, inspect.ismethod) [('b', <bound method C.b of <__main__.C object at 0x100498250>>)] getmembers() returns a list of tuples, where each tuple is (name, member). The second argument to getmembers() is the predicate, which filters

How to find out the default values of a particular function's argument in another function in Python?

安稳与你 提交于 2019-12-01 16:10:15
Let's suppose we have a function like this: def myFunction(arg1='a default value'): pass We can use introspection to find out the names of the arguments that myFunction() takes using myFunction.func_code.co_varnames , but how to find out the default value of arg1 (which is 'a default value' in the above example)? As an alternative to rooting around in the attributes of the function you can use the inspect module for a slightly friendlier interface: For Python 3.x interpreters: import inspect spec = inspect.getfullargspec(myFunction) Then spec is a FullArgSpec object with attributes such as

How to find out the default values of a particular function's argument in another function in Python?

自闭症网瘾萝莉.ら 提交于 2019-12-01 15:16:40
问题 Let's suppose we have a function like this: def myFunction(arg1='a default value'): pass We can use introspection to find out the names of the arguments that myFunction() takes using myFunction.func_code.co_varnames , but how to find out the default value of arg1 (which is 'a default value' in the above example)? 回答1: As an alternative to rooting around in the attributes of the function you can use the inspect module for a slightly friendlier interface: For Python 3.x interpreters: import

How to go about serializing a large, complex object?

北战南征 提交于 2019-12-01 11:22:32
I have a " User " class with 40+ private variables including complex objects like private/public keys (QCA library), custom QObjects etc. The idea is that the class has a function called sign() which encrypts, signs, serializes itself and returns a QByteArray which can then be stored in a SQLite blob. What's the best approach to serialize a complex object? Iterating though the properties with QMetaObject ? Converting it to a protobuf object? Could it be casted to a char array? Could it be casted to a char array? No, because you'd be casting QObject 's internals that you know nothing about,

How to print in REPL the code of functions in Julia?

三世轮回 提交于 2019-12-01 11:04:22
In Julia, a lot of the Base and closer related functions are also written in pure Julia, and the code is easily avaible. One can skim through the repository or the local downloaded files, and see how the function is written/implemented. But I think there is allready some built in method that does that for you, so you can write in REPL or Jupyter Notebook something like: @code functioninquestion() and get something like: functioninquestion(input::Type) some calculations return end without paging throug the code. I just don't remember the method or call. I have read the Reflection/Introspection