[英]Do not understand this line of code in Python?
我正在阅读由一些经验丰富的程序员编写的一些代码,但我不理解其中的一部分。 不幸的是,我是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()
因此,在此示例中:
'bar'
实例化一个list
list('bar') == ['b', 'a', 'r']
set(['b', 'a', 'r']) == {'a', 'b', 'r'}
set
的pop()
方法
{'a', 'b', 'r'}.pop()
将返回'a'
,并将set
保留为{'b', 'r'}
因此,您给定的代码行也是如此:
realworld = ConcreteRealWorldScheduler(RealWorldScenario(newscenario)).schedule()
newscenario
实例化一个新的RealWorldScenario
RealWorldScenario
实例实例化ConcreteRealWorldScheduler
ConcreteRealWorldScheduler
实例的schedule()
方法。 从外面工作,我们有
variable = OneConcreteClass(OneClass(anotherVariable)).method()
要么
variable = SomethingConfusing.method()
我们得出结论, SomethingConfusing
是一个带有称为method
的方法的对象
我们还知道什么? 好吧,真的
OneConcreteClass(OneClass(anotherVariable))
要么
OneConcreteClass(SomethingElseConfusing)
OneConreteClass
因此是一个具体的类,其发生在其另一个目的__init__
方法,具体类型的东西OneClass
已初始化与OneClass(anotherVariable)
有关更多详细信息,请参见Dive into python或此处
在Python中,几乎所有东西都是Object
因此,当我们创建对象的实例时,我们将执行以下操作:
obj = ClassName() # class itself an object of "Type"
或obj = ClassName(Args)#此处将args传递给构造函数
如果您的类具有任何称为method()
成员
您可以按照以下方式进行操作:
obj.method()
要么
ClassName().method()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.