我有一个类,它指定了一组回调函数(此处显示为cb1和cb2).我保留了一些地图,我想在一些事件后打电话给我.
class Foo:
cb1 = None
cb2 = None
def test(self, input):
for (name, callback) in map:
if name == input:
if callback: callback()
...
map = {'one':cb1, 'two':cb2}
def mycallback():
print "mycallback()"
f = Foo()
f.cb1 = mycallback # Register our callback
f.test('one') # Nothing happens
Run Code Online (Sandbox Code Playgroud)
你能发现问题吗?
会发生什么事是,当类初始化时,值的cb1和cb2(这两者都是None)被复制到地图.因此,即使用户'注册'回调(通过分配cb1),地图中的值仍然None没有被调用.
由于Python中没有"引用"这样的东西,我该如何解决这个问题呢?
一个很长的标题,但我希望它具体.标题确实是个问题.即使InvokeMember正在调用的方法有一个out参数并且正在为该参数赋值,我也无法获取该值.这是我最初使用的代码:
string parameter = "";
int result = Convert.ToInt32(typeof(Ability).InvokeMember(selectedMove, BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static, null, null, new object[] { parameter }));
Run Code Online (Sandbox Code Playgroud)
我改变了这个,现在它按预期工作但我不知道为什么:
object[] args = new object[1]; //necessary to retrieve ref/out parameter
int result = Convert.ToInt32(typeof(Ability).InvokeMember(selectedMove, BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static, null, null, args));
Run Code Online (Sandbox Code Playgroud) 可能重复:
C# - "开启类型"有比这更好的选择吗?
考虑经典:
class Widget { }
class RedWidget : Widget { }
class BlueWidget : Widget { }
Run Code Online (Sandbox Code Playgroud)
在大多数情况下,在我的用户界面中,我可以对待所有Widget相同的内容.但是,我需要if或switch通过一些细微差别.
可能的方法:
枚举指标 - 由构造函数设置
enum WidgetVariety { Red, Blue }
class Widget {
public WidgetVariety Variety { get; protected set; }
}
class RedWidget : Widget {
public RedWidget() {
Variety = Red;
}
}
// Likewise for BlueWidget...
switch (mywidget.Variety) {
case WidgetVariety.Red:
// Red specific GUI stuff
case WidgetVariety.Blue:
// …Run Code Online (Sandbox Code Playgroud) 我有一个基本的库,我用它来绘制OpenGL文本,当我使用valgrind确保它是气密的.我一直得到一个不寻常的错误,看起来好像linux c ++库有问题.我想看看你们是否可以发现我的错误或认证我担心的问题,那就是我的c ++库有问题并且需要更换.代码非常简单,但它同时使用OpenGL和FreeImage,因此某些行没有意义.
这是fontsystem.h:
#ifndef FONTSYSTEM_H
#define FONTSYSTEM_H
/*
The Font System works by loading all the font images (max image size 32px^2) into memory and storing
the OpenGL texture ID's into an array that can be access at all times. The DrawString() functions will
search through the string for the specified character requested to draw and then it will draw a quad
and paint the texture on it. Since all the images are pre-loaded, no loading of the …Run Code Online (Sandbox Code Playgroud) 我在许多ose(和一些引导程序)中看到,它们cli在从实模式切换到保护模式之前都禁用了interrupt().为什么我们需要这样做?
我想在Python中编写一个返回n个函数乘法的函数(f1(x) * f2(x) * f3(x) * ... * fn(x)).
我想的是:
def mult_func(*args):
return lambda x: args(0)(x) * args(1)(x) ...
Run Code Online (Sandbox Code Playgroud)
但我不确切知道如何循环nargs中的函数.
谢谢.
platform.machine()
返回机器类型,例如'i386'.如果无法确定值,则返回空字符串.
该字段有哪些可能的值?
在正常情况下,通过省略这些参数来调用具有默认参数的函数。但是,如果我正在即时生成参数,省略一个并不总是那么容易或优雅。有没有办法显式使用函数的默认参数?也就是说,传递一个指向默认参数的参数。
所以像这样的东西除了~use default~被一些智能的东西所取代。
def function(arg='default'):
print(arg)
arg_list= ['not_default', ~use default~ ]
for arg in arg_list:
function(arg=arg)
# output:
# not_default
# default
Run Code Online (Sandbox Code Playgroud)
我不知道这是否可能,并且考虑到术语“默认参数”,我所有的搜索都是编码人员的第一个教程。如果不支持此功能也没关系,我只想知道。
我已经安装gcc-arm-linux-gnu-4.9.1-1.fc20.x86_64在我的 Fedora 20 机器上。
$ cat main.c
int main(void)
{
return 0;
}
$ arm-linux-gnu-gcc main.c
collect2: fatal error: cannot find 'ld'
compilation terminated.
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?这是一个错误吗?
我一直使用“环境变量”这个术语,但我有一位消息灵通的同事一直说“环境变量”。
哪一个是正确的?
python ×4
c# ×2
bootloader ×1
c ×1
c++ ×1
callback ×1
fedora ×1
function ×1
gcc ×1
linux ×1
memory ×1
memory-leaks ×1
osdev ×1
polymorphism ×1
real-mode ×1
reflection ×1
terminology ×1
valgrind ×1
x86 ×1