简体   繁体   English

不了解Python中的这一行代码?

[英]Do not understand this line of code in Python?

I am reading to some piece of code written by some experienced programmer, and I do not understand some part of it. 我正在阅读由一些经验丰富的程序员编写的一些代码,但我不理解其中的一部分。 Unfortunately, I am new to Python programming. 不幸的是,我是Python编程的新手。

This is the line of code which confuses me: 这是使我感到困惑的代码行:

realworld = ConcreteRealWorldScheduler(RealWorldScenario(newscenario)).schedule()

To generalize I will rewrite it again 概括地说,我将再次重写它

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

This part confuses me the most: 这部分最让我感到困惑:

(RealWorldScenario(newscenario))

If someone could give me a thorough description it would be very helpful. 如果有人可以给我详尽的描述,那将非常有帮助。

THanks 谢谢

This is the same as: 这与:

# 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()

It may seem confusing due to the naming, or the complexity of the classes, but this is essentially the same as: 由于命名或类的复杂性,它可能看起来令人困惑,但这与以下内容基本相同:

foo = set(list('bar')).pop()

So, in this example: 因此,在此示例中:

  1. First of all a list is being instantiated with 'bar' 首先用'bar'实例化一个list
    • list('bar') == ['b', 'a', 'r']
  2. Next a set is created from the list 接下来,从列表中创建一个集合
    • set(['b', 'a', 'r']) == {'a', 'b', 'r'}
  3. Then we use set 's the pop() method 然后我们使用setpop()方法
    • {'a', 'b', 'r'}.pop() will return 'a' and leave the set as {'b', 'r'} {'a', 'b', 'r'}.pop()将返回'a' ,并将set保留为{'b', 'r'}

So the same is true of your given line of code: 因此,您给定的代码行也是如此:

realworld = ConcreteRealWorldScheduler(RealWorldScenario(newscenario)).schedule()
  1. First a new RealWorldScenario is instantiated with newscenario 首先,用newscenario实例化一个新的RealWorldScenario
  2. Next, a ConcreteRealWorldScheduler is instantiated with the RealWorldScenario instance 接下来,使用RealWorldScenario实例实例化ConcreteRealWorldScheduler
  3. Finally, the schedule() method of the ConcreteRealWorldScheduler instance is called. 最后,调用ConcreteRealWorldScheduler实例的schedule()方法。

Working from the outside instead, we have 从外面工作,我们有

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

or 要么

variable = SomethingConfusing.method()

We conclude SomethingConfusing is an object with a method called method 我们得出结论, SomethingConfusing是一个带有称为method的方法的对象

What else do we know? 我们还知道什么? Well, it's really 好吧,真的

OneConcreteClass(OneClass(anotherVariable))

or 要么

OneConcreteClass(SomethingElseConfusing)

OneConreteClass is thus a concrete class which takes another object in its __init__ method, specifically something of type OneClass which has been initialised with OneClass(anotherVariable) OneConreteClass因此是一个具体的类,其发生在其另一个目的__init__方法,具体类型的东西OneClass已初始化与OneClass(anotherVariable)

For further details see Dive into python or here 有关更多详细信息,请参见Dive into python此处

In Python almost everything is Object 在Python中,几乎所有东西都是Object

so when we create instance to an object we do something like this: 因此,当我们创建对象的实例时,我们将执行以下操作:

obj = ClassName()  # class itself an object of "Type"

or obj = ClassName(Args) # Here args are passed to the constructor 或obj = ClassName(Args)#此处将args传递给构造函数

if your class has any member called method() 如果您的类具有任何称为method()成员

you can do like as follows: 您可以按照以下方式进行操作:

obj.method()

or 要么

ClassName().method()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM