根据 http://docs.python.org/2/library/functions.html#super ,
If the second argument is omitted, the super object returned is unbound.
哪个是 super (类型)。
我想知道什么是无界的,什么时候是有界的。
最佳答案
您问题的其他答案( answer 、 answer )已经解释了绑定(bind)/未绑定(bind)这两个词的含义。
So my focus is to explain only the use of an unbound proxy object returned from the
super()function (i.e. in the case it was used only with 1 argument).
获取未绑定(bind)对象的原因是以后绑定(bind)它。
特别是,对于从 super() 函数返回的未绑定(bind)对象,您可以使用其 __get__() 方法将其绑定(bind)到适当的对象,例如
super(C).__get__(c) # where C is a class and c is an object
为了说明这一点,让我们创建 3 个依赖类和 3 个对象 - 每个类一个:
class A:
def __init__(self, name):
self.name = name
def message(self, source):
print(f"From: {source}, class: A, object: {self.name}")
class B(A):
def __init__(self, name):
self.name = name
def message(self, source):
print(f"From: {source}, class: B, object: {self.name}")
class C(B):
def __init__(self, name):
self.name = name
def message(self, source):
print(f"From: {source}, class: C, object: {self.name}")
a = A("a")
b = B("b")
c = C("c")
现在在交互式控制台中,首先是为了理解事物:
>>> super(B) # unbounded (note 'None')
<super: __main__.B, None>
>>> super(B).__get__(b) # bounded to object b (note address)
<super: __main__.B, <__main__.B at 0xa9bdac0>>
>>> b # note: the same address
'<__main__.B at 0xa9bdac0>
然后——显示使用不同类/对象组合的结果
(在我们委托(delegate)方法 .message() 的例子中):
>>> super(B).__get__(b).message("super(B)")
From: super(B), class: A, object: b
>>> super(C).__get__(c).message("super(C)")
From: super(C), class: B, object: c
>>> super(B).__get__(c).message("super(B)")
From: super(B), class: A, object: c
最后是绑定(bind)和未绑定(bind)代理到适当类的示例:
>>> A.name = "Class A" # Preparing for it -
>>> B.name = "Class B" # creating some class attributes
>>> super(B).__get__(B).name # Proxy super(B) bounded to class B
'Class A'
>>> super(B).__get__(C).name # Proxy super(B) bounded to class C
'Class A'
>>> super(C).__get__(C).name # Proxy super(C) bounded to class C
'Class B'
关于python - python中的 'super object returned is unbound'是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22403897/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%