小编qur*_*ius的帖子

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

我不明白的是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 …
Run Code Online (Sandbox Code Playgroud)

python oop parameter-passing

7
推荐指数
2
解决办法
3万
查看次数

标签 统计

oop ×1

parameter-passing ×1

python ×1