- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我不明白的是b = Bar(a)
。它有什么作用? Bar
如何将 a
作为参数?
那不是说 Bar
继承自 a
吗?什么是 Bar.Foo1 = Foo
?这是否意味着 Foo1
是类 Foo()
的一个实例?当 Foo1
本身是一个对象时,我们如何访问它? b.arg.variable
是什么意思?这是否意味着 b
有一个方法 arg
,它有一个名为 variable
的变量?以下代码来自this answer
我只是找不到解析对象作为另一个类的参数。
class Foo (object):
#^class name #^ inherits from object
bar = "Bar" #Class attribute.
def __init__(self):
# #^ The first variable is the class instance in methods.
# # This is called "self" by convention, but could be any name you want.
self.variable="Foo" #instance attribute.
print self.variable, self.bar #<---self.bar references class attribute
self.bar = " Bar is now Baz" #<---self.bar is now an instance attribute
print self.variable, self.bar
def method(self,arg1,arg2):
#This method has arguments. You would call it like this : instance.method(1,2)
print "in method (args):",arg1,arg2
print "in method (attributes):", self.variable, self.bar
a=Foo() # this calls __init__ (indirectly), output:
# Foo bar
# Foo Bar is now Baz
print a.variable # Foo
a.variable="bar"
a.method(1,2) # output:
# in method (args): 1 2
# in method (attributes): bar Bar is now Baz
Foo.method(a,1,2) #<--- Same as a.method(1,2). This makes it a little more explicit what the argument "self" actually is.
class Bar(object):
def __init__(self,arg):
self.arg=arg
self.Foo1=Foo()
b=Bar(a)
b.arg.variable="something"
print a.variable # something
print b.Foo1.variable # Foo
最佳答案
“我不明白的是 b = Bar(a)。它有什么作用?”
b = Bar(a)
做了两件事。首先,它创建了一个 Bar
类的对象(附加了任何类变量和方法)。然后,它运行 __init__
,第一个参数 (self
) 指向刚创建的对象,第二个参数 a
( 参数
)。在运行 __init__
时,作为该方法中的命令之一,它将 self.arg
设置为指向 arg
指向的对象(即到变量 a
指向的对象)。最后,变量 b
被设置为引用创建的对象。
这样想可能会有所帮助:Python 中的变量实际上只是指向对象的指针。您可以有多个变量指向同一个对象。在这种情况下,a
和 b.arg
都指向同一个对象。
一开始我也发现这种事情很困惑。我曾看到建议将变量视为与其指向的对象分开的概念,并忽略了它,因为它不必要地使事情复杂化,但我不得不重新接受这种思维方式才能理解事物。人们确实经常使用变量作为名称来引用它所指向的对象;你只需要知道什么时候该照字面意思或不照字面意思理解。
“这不是说 Bar 继承自 a 吗?”
没有。如果 a
是一个类,那么 class Bar(a)
就意味着 Bar 继承自 a
。但在b = Bar(a)
,a
是作为参数传递给 __init__
的对象。
“什么是 Bar.Foo1 = Foo?”
抱歉 - 我在您提供的示例代码中没有看到这一点。
“b.arg.variable是什么意思?”
b
是一个对象(我的意思是,b
指的是一个对象)并且 b.arg
是该对象的属性之一.方法和变量是不同类型的属性。在这种情况下,b.arg
是一个指向对象的变量。 b.arg
引用的对象具有属性variable
,这是一个变量。
b.arg
指的是 a
指的同一个对象,因此 b.arg.variable
是与 相同的变量>a.变量
。它不仅指向同一个对象,而且实际上是同一个变量。它指向的对象是字符串"something"
。
@Brenbarn:我认为这就是 quirius 的意思“那不是说 Bar 继承自 a 吗?”。
关于python - 将类的实例(类的对象)传递给另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24223722/
我的一位教授给了我们一些考试练习题,其中一个问题类似于下面(伪代码): a.setColor(blue); b.setColor(red); a = b; b.setColor(purple); b
我似乎经常使用这个测试 if( object && object !== "null" && object !== "undefined" ){ doSomething(); } 在对象上,我
C# Object/object 是值类型还是引用类型? 我检查过它们可以保留引用,但是这个引用不能用于更改对象。 using System; class MyClass { public s
我在通过 AJAX 发送 json 时遇到问题。 var data = [{"name": "Will", "surname": "Smith", "age": "40"},{"name": "Wil
当我尝试访问我的 View 中的对象 {{result}} 时(我从 Express js 服务器发送该对象),它只显示 [object][object]有谁知道如何获取 JSON 格式的值吗? 这是
我有不同类型的数据(可能是字符串、整数......)。这是一个简单的例子: public static void main(String[] args) { before("one"); }
嗨,我是 json 和 javascript 的新手。 我在这个网站找到了使用json数据作为表格的方法。 我很好奇为什么当我尝试使用 json 数据作为表时,我得到 [Object,Object]
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我听别人说 null == object 比 object == null check 例如: void m1(Object obj ) { if(null == obj) // Is thi
Match 对象 提供了对正则表达式匹配的只读属性的访问。 说明 Match 对象只能通过 RegExp 对象的 Execute 方法来创建,该方法实际上返回了 Match 对象的集合。所有的
Class 对象 使用 Class 语句创建的对象。提供了对类的各种事件的访问。 说明 不允许显式地将一个变量声明为 Class 类型。在 VBScript 的上下文中,“类对象”一词指的是用
Folder 对象 提供对文件夹所有属性的访问。 说明 以下代码举例说明如何获得 Folder 对象并查看它的属性: Function ShowDateCreated(f
File 对象 提供对文件的所有属性的访问。 说明 以下代码举例说明如何获得一个 File 对象并查看它的属性: Function ShowDateCreated(fil
Drive 对象 提供对磁盘驱动器或网络共享的属性的访问。 说明 以下代码举例说明如何使用 Drive 对象访问驱动器的属性: Function ShowFreeSpac
FileSystemObject 对象 提供对计算机文件系统的访问。 说明 以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读
我是 javascript OOP 的新手,我认为这是一个相对基本的问题,但我无法通过搜索网络找到任何帮助。我是否遗漏了什么,或者我只是以错误的方式解决了这个问题? 这是我的示例代码: functio
我可以很容易地创造出很多不同的对象。例如像这样: var myObject = { myFunction: function () { return ""; } };
function Person(fname, lname) { this.fname = fname, this.lname = lname, this.getName = function()
任何人都可以向我解释为什么下面的代码给出 (object, Object) 吗? (console.log(dope) 给出了它应该的内容,但在 JSON.stringify 和 JSON.parse
我正在尝试完成散点图 exercise来自免费代码营。然而,我现在只自己学习了 d3 几个小时,在遵循 lynda.com 的教程后,我一直在尝试确定如何在工具提示中显示特定数据。 This code
我是一名优秀的程序员,十分优秀!