为什么创建/修改locals()的成员在函数内不起作用?
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
Run Code Online (Sandbox Code Playgroud) 必须有一个简单的方法来做到这一点,但我找不到它.
我可以在Tcl中运行什么命令来使其内省并报告正在运行的自身版本?
从拍摄前一后进行一些修改,以回应sepp2k的关于命名空间的评论,我已经实现字符串#to_class方法.我在这里分享代码,我相信它可能会被重构,特别是"i"计数器.您的意见表示赞赏.
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 the last element,
# as the last element should be a class
klass.is_a?(Class) || (klass.is_a?(Module) and i != chain.length) ? klass : nil
end
rescue NameError
nil
end
end
#Tests that should be passed.
assert_equal(Fixnum,"Fixnum".to_class)
assert_equal(M::C,"M::C".to_class)
assert_nil …Run Code Online (Sandbox Code Playgroud) 除了使用带有调试器的完全集成的IDE(如Eclipse)之外,还有什么小工具可以实现这个目的:
它不需要抛光,甚至不是绝对稳定的,它可以是像wx这样的小部件库的内省示例代码.平台独立会很好(不是PyObjC程序,或类似的东西在Windows上).
有任何想法吗 ?
编辑:是的,我知道pdb,但我正在寻找所有当前对象的图形树.
不过,这里有一个很好的介绍如何使用pdb(在这种情况下在Django中): pdb + Django
我想通过此函数的泛化自动确定给定Javascript对象中的所有属性(包括隐藏的属性):
function keys(obj) {
var ll = [];
for(var pp in obj) {
ll.push(pp);
}
return ll;
}
Run Code Online (Sandbox Code Playgroud)
这适用于用户定义的对象,但许多内置程序都失败了:
repl> keys({"a":10,"b":2}); // ["a","b"]
repl> keys(Math) // returns nothing!
Run Code Online (Sandbox Code Playgroud)
基本上,我想编写Python的dir()和help()的等价物,它们在探索新对象时非常有用.
我的理解是只有内置对象具有隐藏属性(用户代码显然不能设置"可枚举"属性直到HTML5),因此一种可能性是简单地将Math,String等属性硬编码为dir()等价物(使用这里的列表).但有更好的方法吗?
编辑:好的,到目前为止我看到的最佳答案是在这个帖子上.你不能用你自己的JS代码轻松做到这一点,但最好的办法是在Chrome的开发者工具(Chrome - > View - > Developer - > Developer Tools)中使用console.dir.运行console.dir(Math)并单击三角形向下钻取以列出所有方法.这对于大多数交互/发现工作来说已经足够好了(你真的不需要在运行时这样做).
我需要在Ruby 1.8中获取词法封闭方法的名称; 例如
def foo
this_method = __callee__ # => 'foo'
end
Run Code Online (Sandbox Code Playgroud)
上面的代码在Ruby 1.9中有效,但在1.8中失败,因为1.9中引入了__callee__.
有没有在1.8中做到这一点的建议?内核#caller看起来很有前途,但似乎给了我调用堆栈,从方法的调用者开始,而不是方法本身.
我想我可以抛出一个异常,抓住它,并抓住Exception #backtrace数组中的第一个元素,但我的直觉告诉我将会很慢.
有这样的方法.
- (void)method: (CustomClass)param;
Run Code Online (Sandbox Code Playgroud)
CustomClass继承自NSObject.
我在m下面有一个变量,它是该Method方法的结构.我调用method_getArgumentType()以获取这样的参数类型:
char szArgType[100] = {0,};
Method m = ...;
...
method_getArgumentType(m, 2, szArgType, 100);
Run Code Online (Sandbox Code Playgroud)
我打印了szArgType.它打印@,但我想打印CustomClass.有没有一种很好的方法从Method运行时获取参数的真实对象类类型?
我正面临着这样的情况:我发现在基类中存储对象的类型(作为枚举)以进一步将指向该基类的指针转换为子类指针(取决于该类型的值).
例如 :
class CToken
{
public:
token_type_e eType;
};
class COperatorToken : public CToken
{
public:
// Sub class specific stuff
};
class CLiteralToken : public CToken
{
public:
// Sub class specific stuff
};
Run Code Online (Sandbox Code Playgroud)
然后
vector<CToken *> aTokens;
//...
for( size_t nI = 0, nMaxI = aTokens.size(); nI < nMaxI; ++nI )
{
switch( aTokens[ nI ]->eType )
{
case E_OPERATOR :
// Do something with sub-class specific stuff.
break;
case E_LITERAL :
// Do something with sub-class …Run Code Online (Sandbox Code Playgroud) 在Julia中,许多Base和更接近的相关函数也是用纯Julia编写的,代码很容易实现.可以浏览存储库或本地下载的文件,并查看函数的编写/实现方式.但我认为已经有一些内置的方法可以为你做到这一点,所以你可以在REPL或Jupyter Notebook中编写如下内容:
@code functioninquestion()
得到类似的东西:
functioninquestion(input::Type)
some calculations
return
end
没有分页通过代码.我只是不记得方法或电话.我已经阅读了手册的反思/反思部分,但我似乎无法在那里使用任何东西.我试过methods,methodswith,code_lowered,expand,似乎无法让他们给我want-
我试图在运行时弄清楚类的属性是否可以为空.例如:
@interface A : NSObject
+ (NSSet<NSString *> *)nullableProperties;
@property (readonly, nonatomic) NSString *description;
@property (readonly, nonatomic, nullable) NSError *error;
@end
@implementation A
+ (NSSet<NSString *> *)nullableProperties {
// Code that identifies nullable properties.
}
@end
Run Code Online (Sandbox Code Playgroud)
nullableProperties应该在这种情况下,返回一个NSSet有@"error".
property_getAttributesfunction可以提供一些属性的信息(这里有更多信息).不幸的是,它没有提供有关该属性是否被声明为可空的信息.
我想避免nullableProperties为我需要知道的可空属性的每个类实现.
introspection ×10
python ×3
objective-c ×2
ruby ×2
c++ ×1
casting ×1
debugging ×1
hidden ×1
javascript ×1
julia ×1
nullable ×1
object ×1
polymorphism ×1
scope ×1
tcl ×1
version ×1