【问题标题】:Passing an instance of a class (object of a class) to another class将类的实例(类的对象)传递给另一个类
【发布时间】:2014-06-14 19:53:31
【问题描述】:

我不明白的是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

【问题讨论】:

  • 您应该阅读 the Python tutorial 以熟悉 Python 的基础知识。
  • 我读到了。没有将对象传递给另一个类这样的事情。而且我无法从其他来源获得
  • 它被描述为here。 Python 中的一切都是对象。将对象传递给类与传递整数(如该示例)、字符串或其他任何东西没有什么不同。
  • 那么b是Bar类的一个对象,会调用其构造函数来初始化b,a`s(Foo())还是Bar()?什么是 b.arg.variable?
  • 我不明白你在问什么。 a 已经用a = Foo() 行创建,当时调用了Foo 的__init__。当Bar(a)被执行时,就会调用Bar的__init__。您发布的示例中的 cmets 似乎已经大部分回答了您提出的问题。

标签: python oop


【解决方案1】:

“我不明白的是b = Bar(a)。它有什么作用?”

b = Bar(a) 做了两件事。首先,它创建一个类Bar 的对象(附加任何类变量和方法)。然后,它运行__init__,第一个参数(self)指向刚刚创建的对象,a 作为第二个参数(arg)。在运行__init__ 时,作为该方法中的命令之一,它将self.arg 设置为指向arg 指向的对象(即指向变量a 指向的对象)。最后,变量b 设置为引用创建的对象。

这样想可能会有所帮助:Python 中的变量实际上只是一个指向对象的指针。您可以有多个变量指向同一个对象。在这种情况下,ab.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.variablea.variable 是同一变量。它不仅指向同一个对象,而且实际上是同一个变量。它指向的对象是字符串"something"

@Brenbarn:我认为这就是 quirius 所说的“这不是意味着 Bar 继承自 a 吗?”的意思。

【讨论】:

    【解决方案2】:

    这是一个更简单的例子。假设你有这些类:

    class Foo(object):
        pass
    
    class Bar(object):
        def __init__(self, arg):
            self.arg = arg
    

    您可以做以下两件事:

    # Option 1: pass in an integer
    >>> b = Bar(1)
    >>> b.arg
    1
    
    # Option 2: pass in a Foo object
    >>> a = Foo()
    >>> b = Bar(a)
    >>> b.arg
    <__main__.Foo object at 0x0000000002A440F0>
    

    这两种情况的处理方式没有区别。传入a(一个 Foo 对象)与传入一个整数没有什么不同。 Bar 所做的只是存储传入的值,无论它是 Foo 还是 int,它都可以存储相同的值。当你调用Bar(something)时,完全取决于Bar类如何处理传入的对象。传入对象的类型不涉及,除非Bar选择显式涉及(例如,通过调用传入对象的方法)。

    【讨论】:

    • 谢谢。我很抱歉问了一些琐碎的问题,但我无法摆脱 Java 框架,将变量视为变量而不是对象。
    • @qurius:如果我理解正确,现在看来您是在询问Bar(a) 是否创建 一个以某种方式“基于”a 的新对象(例如,a 的副本)。提到这一点可能很好! :-) 当你只问“Bar(a) 是做什么的”时,不清楚你不明白的地方。
    • 是的,这确实是我要问的。 :)
    • 那么类定义中的(object) 部分真的需要吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-04
    • 2023-01-25
    • 1970-01-01
    • 1970-01-01
    • 2015-05-13
    • 2014-04-03
    相关资源
    最近更新 更多