不理解Python中的这行代码吗?

不理解Python中的这行代码吗?,python,class,oop,abstract-class,Python,Class,Oop,Abstract Class,我正在阅读一些有经验的程序员编写的代码,但我不理解其中的某些部分。不幸的是,我不熟悉Python编程 这是让我困惑的一行代码: realworld = ConcreteRealWorldScheduler(RealWorldScenario(newscenario)).schedule() 概括地说,我将再次重写它 variable = OneConcreteClass(OneClass(anotherVariable)).method() 这一部分最让我困惑: (RealWorldScen

我正在阅读一些有经验的程序员编写的代码,但我不理解其中的某些部分。不幸的是,我不熟悉Python编程

这是让我困惑的一行代码:

realworld = ConcreteRealWorldScheduler(RealWorldScenario(newscenario)).schedule()
概括地说,我将再次重写它

variable = OneConcreteClass(OneClass(anotherVariable)).method()
这一部分最让我困惑:

(RealWorldScenario(newscenario))
如果有人能给我一个详细的描述,那将是非常有帮助的

谢谢

这与:

# New object, newscenario passed to constructor
world_scenario = RealWordScenario(newscenario)
# Another new object, now world_scenario is passed to constructor
scheduler = ConcreteRealWorldScheduler(world_scenario)
# Call the method
variable = scheduler.method()
foo = set(list('bar')).pop()
这与:

# New object, newscenario passed to constructor
world_scenario = RealWordScenario(newscenario)
# Another new object, now world_scenario is passed to constructor
scheduler = ConcreteRealWorldScheduler(world_scenario)
# Call the method
variable = scheduler.method()
foo = set(list('bar')).pop()

相反,我们必须从外部工作

variable = OneConcreteClass(OneClass(anotherVariable)).method()

我们的结论是,
somethingfuzzing
是一个对象,其方法名为
method

我们还知道什么? 嗯,真的

OneConcreteClass(OneClass(anotherVariable))

因此,
OneConreteClass
是一个具体的类,它在其
\uuuu init\uuuuu
方法中接受另一个对象,特别是
OneClass
类型的对象,它已经用
OneClass(另一个变量)


有关更多详细信息,请参见或

从外部工作,我们有

variable = OneConcreteClass(OneClass(anotherVariable)).method()

我们的结论是,
somethingfuzzing
是一个对象,其方法名为
method

我们还知道什么? 嗯,真的

OneConcreteClass(OneClass(anotherVariable))

因此,
OneConreteClass
是一个具体的类,它在其
\uuuu init\uuuuu
方法中接受另一个对象,特别是
OneClass
类型的对象,它已经用
OneClass(另一个变量)


有关更多详细信息,请参见或

在Python中,几乎所有内容都是
对象

因此,当我们为对象创建实例时,我们会执行以下操作:

obj = ClassName()  # class itself an object of "Type"
或 obj=ClassName(Args)#这里Args被传递给构造函数

如果您的类有任何名为
method()

您可以按如下方式进行操作:

obj.method()


在Python中,几乎所有内容都是
Object

因此,当我们为对象创建实例时,我们会执行以下操作:

obj = ClassName()  # class itself an object of "Type"
或 obj=ClassName(Args)#这里Args被传递给构造函数

如果您的类有任何名为
method()

您可以按如下方式进行操作:

obj.method()


由于类的命名或复杂性,它可能看起来很混乱,但本质上与以下内容相同:

# New object, newscenario passed to constructor
world_scenario = RealWordScenario(newscenario)
# Another new object, now world_scenario is passed to constructor
scheduler = ConcreteRealWorldScheduler(world_scenario)
# Call the method
variable = scheduler.method()
foo = set(list('bar')).pop()
因此,在本例中:

  • 首先,一个
    列表
    被实例化为
    'bar'
    • list('bar')=['b','a','r']
  • 接下来,从列表中创建一个集合
    • set(['b','a','r'])=={'a','b','r'}
  • 然后我们使用
    set
    pop()
    方法
    • {'a',b',r'}.pop()
      将返回
      'a'
      ,并将
      集保留为
      {'b',r'}
  • 因此,给定的代码行也是如此:

    realworld = ConcreteRealWorldScheduler(RealWorldScenario(newscenario)).schedule()
    
  • 首先,一个新的
    现实世界场景
    newscenario
  • 接下来,用
    RealWorldScenario
    实例实例化一个
    ConcreteRealWorldScheduler
  • 最后,调用
    ConcreteRealWorldScheduler
    实例的
    schedule()
    方法

  • 由于类的命名或复杂性,它可能看起来很混乱,但本质上与以下内容相同:

    # New object, newscenario passed to constructor
    world_scenario = RealWordScenario(newscenario)
    # Another new object, now world_scenario is passed to constructor
    scheduler = ConcreteRealWorldScheduler(world_scenario)
    # Call the method
    variable = scheduler.method()
    
    foo = set(list('bar')).pop()
    
    因此,在本例中:

  • 首先,一个
    列表
    被实例化为
    'bar'
    • list('bar')=['b','a','r']
  • 接下来,从列表中创建一个集合
    • set(['b','a','r'])=={'a','b','r'}
  • 然后我们使用
    set
    pop()
    方法
    • {'a',b',r'}.pop()
      将返回
      'a'
      ,并将
      集保留为
      {'b',r'}
  • 因此,给定的代码行也是如此:

    realworld = ConcreteRealWorldScheduler(RealWorldScenario(newscenario)).schedule()
    
  • 首先,一个新的
    现实世界场景
    newscenario
  • 接下来,用
    RealWorldScenario
    实例实例化一个
    ConcreteRealWorldScheduler
  • 最后,调用
    ConcreteRealWorldScheduler
    实例的
    schedule()
    方法

  • RealWorldScenario(newscenario)
    意味着
    RealWorldScenario
    类的对象正在用变量
    newscenario
    构造。我不知道为什么内部实例化会让你感到困惑,但外部实例化不会。你知道python中的类和
    \uuu init\uuuu
    方法吗?
    RealWorldScenario(newscenario)
    意味着
    RealWorldScenario
    类的对象正在使用变量
    newscenario
    构造。我不知道为什么内部实例化会让您感到困惑,但外部实例化不会。您知道python中的类和
    \uuu init\uu
    方法吗?