简体   繁体   English

Python将类对象传递给anther类

[英]Python Passing Class Object in to anther Class

If I have two files class1_file.py and class2_file.py and each of them has got classes class1 and class2 . 如果我有两个文件class1_file.pyclass2_file.py并且每个文件都有类class1class2

class1_file.py class1_file.py

class class1:
    def dummy(self, class2):
        class2.dummy()

class2_file.py: class2_file.py:

class class2
    def dummy(self):
        print 'hey'

Will I need to include: from class2_file import class2 in to class1_file.py for the method in class1 dummy to work? 我是否需要包括: from class2_file import class2class1_file.py中,class1 dummy的方法可以工作吗? I am a bit confused as I came from strong type language which requires to import classes and passing them into methods for use. 我有点困惑,因为我来自强类型语言,它需要导入类并将它们传递给方法以供使用。 But for python it seems the argument can be of any type and it will resolve it at compilation. 但对于python,似乎参数可以是任何类型,它将在编译时解析它。 If it is not required, how does the method in class1 know what is coming in to the function dummy for class1 ? 如果不需要它,如何在方法class1知道什么是进来的功能dummyclass1

no you don't have to, because in signature def dummy(self, class2): class2 is just a local variable in method dummy . 不,你不必,因为在签名def dummy(self, class2): class2只是方法dummy的局部变量。 It can also be def dummy(self, foo): as long as foo is an object with method dummy . 它也可以是def dummy(self, foo):只要foo是一个带有方法dummy的对象。 See Duck typing in Python . 请参阅Python中的Duck输入

Don't name the variable class2. 不要将变量命名为class2。 And yes, you will need to import class2 from class2_file. 是的,您需要从class2_file导入class2。

from class2_file import class2

class class1:
    def dummy(self, class2_obj):
        class2_obj.dummy()

If your intention is to pass an object of class2 to class1 's dummy , then to create an object of class2 you need to import class2. 如果你的目的是将class2的对象传递给class1dummy ,那么要创建class2的对象,你需要导入class2。

from class2_file import class2
class class1:
    def dummy(self, obj):      # To avoid confusion changed the variable name
        obj.dummy()

class1().dummy(class2())       # import is needed for `class2()` part only

Apart from that, class2 is just a variable name, that could be anything. 除此之外, class2只是一个变量名,可以是任何东西。 That is why I changed it to obj in this answer. 这就是我在这个答案中把它改成obj的原因。

When you do class2() Python will look for the definition of class2 in all the valid scopes and it will not be found. 当你执行class2() Python会在所有有效范围内查找class2的定义,但是找不到它。 But when you do 但是,当你这样做

from class2_file import class2

you are introducing class2 as a local name in this module. 您将在此模块中将class2作为本地名称引入。 So, Python would be able to recognize it. 因此,Python将能够识别它。

    def dummy(self, class2):
        class2.dummy()

When you create a function like this, Python will try to execute class2.dummy() and it will class2 as the parameter, not as the class, because class2 is the nearest name in scope. 当您创建这样的函数时,Python将尝试执行class2.dummy() ,它将class2作为参数,而不是类,因为class2是范围中最接近的名称。 And as long as the value passed for class2 has dummy method, that line will be evaluated properly. 只要为class2传递的值具有dummy方法,就会正确评估该行。

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

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