本文有一个片段__bases__,通过向类继承的类的现有类集合添加一个类来动态更改某些Python代码的继承层次结构.好的,这很难读,代码可能更清晰:
class Friendly:
def hello(self):
print 'Hello'
class Person: pass
p = Person()
Person.__bases__ = (Friendly,)
p.hello() # prints "Hello"
Run Code Online (Sandbox Code Playgroud)
也就是说,Person不从Friendly源级别继承,而是通过修改__bases__Person类的属性在运行时动态添加此继承关系.但是,如果您更改Friendly并Person成为新的样式类(通过继承自object),则会收到以下错误:
TypeError: __bases__ assignment: 'Friendly' deallocator differs from 'object'
Run Code Online (Sandbox Code Playgroud)
关于这一点的谷歌搜索似乎表明,在运行时更改继承层次结构时,新样式类和旧样式类之间存在一些不兼容性.具体来说:"新式类对象不支持赋予其基础属性".
我的问题是,是否有可能通过使用__mro__属性使上述Friendly/Person示例在Python 2.7+中使用新式类工作?
免责声明:我完全意识到这是一个模糊的代码.我完全意识到,在实际的生产代码中,这样的技巧往往难以理解,这纯粹是一个思想实验,并且可以让人们了解Python如何处理与多重继承相关的问题.
我正在尝试向表中添加行tbody.但我遇到了实现这个问题的问题.首先,从html页面更改下拉列表时调用发生所有事情的功能.我创建了一个tr包含所有td内部的字符串,其中包含html元素,文本和其他内容.但是当我尝试使用以下方法将生成的行添加到表中时:
$(newRowContent).appendTo("#tblEntAttributes tbody");
Run Code Online (Sandbox Code Playgroud)
我遇到了一个错误.表的名称是tblEntAttributes,我正在尝试将其添加到tbody.
实际上正在发生的事情是jQuery无法tblEntAttributes作为一个HTML元素.但是我可以使用它来访问它documemt.getElementById("tblEntAttributes");
有没有什么办法可以通过向tbody表中添加行来实现这一点.也许绕道或其他东西.
这是整个代码:
var newRowContent = "<tr><td><input type=\"checkbox\" id=\"" + chkboxId + "\" value=\"" + chkboxValue + "\"></td><td>" + displayName + "</td><td>" + logicalName + "</td><td>" + dataType + "</td><td><input type=\"checkbox\" id=\"chkAllPrimaryAttrs\" name=\"chkAllPrimaryAttrs\" value=\"chkAllPrimaryAttrs\"></td><td><input type=\"checkbox\" id=\"chkAllPrimaryAttrs\" name=\"chkAllPrimaryAttrs\" value=\"chkAllPrimaryAttrs\"></td></tr>";
$("#tblEntAttributes tbody").append(newRowContent);
Run Code Online (Sandbox Code Playgroud)
我忘记提到的一件事是编写此代码的函数实际上是ajax调用的成功回调函数.我能够访问该表使用document.getElementById("tblEntAttributes")但由于某种原因$(#tblEntAttributes)似乎不起作用.
如果我dynamic在Visual Studio的即时窗口中使用,我会收到错误
未定义或导入预定义类型"Microsoft.CSharp.RuntimeBinder.Binder"
我该如何解决这个问题?
飞镖规格说明:
已知类型信息反映了运行时对象的类型,并且可能始终通过动态类型检查构造(其他语言中的instanceOf,casts,typecase等的类比)来查询.
听起来不错,但是没有instanceof类似的操作员.那么我们如何在Dart中执行运行时类型检查?有可能吗?
我在一些帖子中看到人们document.write()在编写动态HTML时不满意使用javascript.
为什么是这样?什么是正确的方法?
我有以下功能:
public static T TryGetArrayValue<T>(object[] array_, int index_)
{
... //some checking goes up here not relevant to question
dynamic boxed = array_[index_];
return (T)boxed;
}
Run Code Online (Sandbox Code Playgroud)
当我用以下方式调用它时,
object a = new object();
object v = TUtils.TryGetArrayValue<object>(new object[] { a }, 0);
Run Code Online (Sandbox Code Playgroud)
(T)boxed 抛出空引用异常.
除了"对象"之外,我放在那里的任何其他类型,它完全正常.
任何想法是什么,为什么它抛出异常?
编辑:我使用动态的原因是为了避免在转换类型时出现异常,例如:
double a = 123;
int v = TUtils.TryGetArrayValue<int>(new object[] { a }, 0);
Run Code Online (Sandbox Code Playgroud) 我想基于其单元格高度的总和从另一个viewcontroller更改tableview的高度,因为它们是动态的.它可能吗?谢谢
添加在:
我基本上拥有一个UserProfileViewController,它在屏幕的一半上有一个容器视图.在那里我添加了不同的其他viewcontrollers:


在wall按钮的情况下,这是我添加viewcontroller的方式,它是后续的tableview:
- (IBAction)wallButtonPressed:(id)sender
{
//Check if there is an instance of the viewcontroller we want to display. If not make one and set it's tableview frame to the container's view bounds
if(!_userWallViewController) {
self.userWallViewController = [[WallViewController alloc] init];
// self.userWallViewController.activityFeedTableView.frame = self.containerView.bounds;
}
[self.userWallViewController.containerView addSubview:self.userWallViewController.activityFeedTableView];
//If the currentviewcontroller adn it's view are already added to the hierarchy remove them
[self.currentViewController.view removeFromSuperview];
[self.currentViewController removeFromParentViewController];
//Add the desired viewcontroller to the currentviewcontroller
self.currentViewController = self.userWallViewController;
//Pass the data needed …Run Code Online (Sandbox Code Playgroud) 请有经验的PHP帮助以下人员.在我的代码中的某个地方,我在非实例化的类中调用了一个公共静态方法:
$result = myClassName::myFunctionName();
Run Code Online (Sandbox Code Playgroud)
但是,我希望有很多这样的类,并根据用户的语言动态确定正确的类名.换句话说,我有:
$language = 'EN';
Run Code Online (Sandbox Code Playgroud)
......我需要做一些事情:
$result = myClassName_EN::myFunctionName();
Run Code Online (Sandbox Code Playgroud)
我知道我可以将语言作为参数传递给函数并在一个公共类中处理它,但由于各种原因,我宁愿选择不同的解决方案.
这对任何人都有意义吗?谢谢.
dynamic ×10
.net ×2
c# ×2
height ×2
class ×1
dart ×1
dart-mirrors ×1
function ×1
html ×1
html-table ×1
inheritance ×1
instanceof ×1
ios ×1
ios7 ×1
javascript ×1
jquery ×1
mysql ×1
php ×1
python ×1
rows ×1
uitableview ×1
uitextview ×1