将类的实例(类的对象)传递给python中的另一个类

qur*_*ius 7 python oop parameter-passing

我不明白的是b = Bar(a).它有什么作用?Bar如何作为一个论点?不是说Bar继承自?什么是Bar.Foo1 = Foo?这是否意味着Foo1是类Foo()的实例?当它本身是一个对象时,我们如何访问Foo1?b.arg.variable是什么意思?难道它不意味着b有一个方法arg,它有一个变量叫做变量吗?以下代码来自 答案

如果问题是天真的,请原谅我.我只是找不到解析对象作为另一个类的参数.任何澄清都会很棒!

  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
Run Code Online (Sandbox Code Playgroud)

Bre*_*arn 11

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

class Foo(object):
    pass

class Bar(object):
    def __init__(self, arg):
        self.arg = arg
Run Code Online (Sandbox Code Playgroud)

您可以执行以下两项操作:

# 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>
Run Code Online (Sandbox Code Playgroud)

这两种情况的处理方式没有区别。顺便a(Foo对象)是从传入的整数没有什么不同。所有这一切Bar确实是存储在传递的价值,它可以存储它一样的无论是富或int。当您调用 时Bar(something),完全取决于Bar类如何处理传入的对象。不涉及传入对象的类型,除非Bar选择显式涉及它(例如,通过调用传入对象的方法)目的)。


Lin*_*ist 9

"我不明白的是b = Bar(a).它做了什么?"

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

这样思考可能会有所帮助:Python中的变量实际上只是指向对象的指针.您可以有多个指向同一对象的变量.在这种情况下,a并且b.arg都指向同一个对象.

起初我发现这种事情也很混乱.我已经看到了将变量视为与它们所指向的对象分离的概念的建议,并将其视为不必要地使事情复杂化,但我不得不回过头来接受这种思维方式以便理解事物.人们经常使用变量作为名称来引用它指向的对象; 你只需知道何时从字面上理解.

"这不意味着Bar继承了吗?"

不.如果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.variable.它不仅指向同一个对象,而且实际上是同一个变量.它指向的对象是字符串"something".

@Brenbarn:我认为这就是quirius所说的"不是那个意味着Bar继承自?".