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