gpt4 book ai didi

python - 如何将整个类作为参数传递给另一个类的方法

转载 作者:太空宇宙 更新时间:2023-11-04 09:10:19 25 4
gpt4 key购买 nike

是否可以将整个类(而不是实例)作为参数传递给 Python 中的另一个类方法?如果我首先有该类的多个实例,并且需要将其中任何一个传递给第二类的方法而不指定哪个实例,我可以这样做吗:

class First():
def __init__(self, a, b):
pass

class Second():
def __init__(self, c, d):
pass
def method(self, First):
#and how do I call here the whole class First
#without calling a particular instance here?

最佳答案

简单明了。

def method(self, First):
First() #instantiation
First.classmethod()
First.staticmethod()

在 python 中,类本身就是对象,因此您可以像这样调用您的方法

second_instance.method(Any_Class_You_Want)

关于python - 如何将整个类作为参数传递给另一个类的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16093133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com