Reputation: 81
Is it possible to pass a whole class (not an instance) as a parameter to another class method in Python? If I have several instances of the class first and need to pass any of them not specifying which one to method of class Second, can I do something like this:
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?
Upvotes: 2
Views: 8608
Reputation: 978
You can do:
class Model1:
def get():
return '1'
class Model2:
def get(Model1):
print('test: '+ str(Model1.get()))
if __name__ == '__main__':
Model2.get(Model1)
the output is;: test: 1
Upvotes: 0
Reputation: 365737
First, you don't need to specify types in Python. So, if you want method
to take a First
instance, just do this:
class Second():
def __init__(self, c, d):
pass
def method(self, first):
pass
my_first = First(0, 1)
my_second = Second(2, 3)
my_second.method(my_first)
I believe that answers your real question, which is:
If I have several instances of the class first and need to pass any of them not specifying which one to method of class Second…
If you want to ensure that the parameter actually is a First, you can always add an assert isinstance(first, First)
or if not isinstance(first, First): raise TypeError
or whatever, but usually you don't want to do that in Python. The whole point of "duck typing" is that you write a function that takes "anything that acts like a First
instance" rather than a function that takes "a First
instance".
You then say:
Now I need to mutate variables from the First class inside the method of a second class:
So… just do it. Your example doesn't have any attributes in the First
class, so let's add some:
class First():
def __init__(self, a, b):
self.total = a + b
And now, let's use them in Second.method
:
class Second():
def __init__(self, c, d):
self.total = c + d
def method(self, first):
first.total += self.total
So:
>>> my_first = First(0, 1)
>>> my_first.total
1
>>> my_second = Second(2, 3)
>>> my_second.total
5
>>> my_first.total += 2
>>> my_first.total
3
>>> my_second.method(my_first)
>>> my_first.total
8
Or, if you meant that you wanted to mutate the class attributes in class First
… you don't even need a First
instance for that:
First.my_class_attribute = 1
If you really do need to pass a class itself… well, a class is a regular value like anything else:
class Second():
def __init__(self, c, d):
pass
def method(self, cls):
pass
my_second = Second(1, 2)
my_second.method(First)
And you can access the class attributes of cls
from within method
, just as easily as you can access instance attributes when an instance is passed.
Upvotes: 3
Reputation: 26941
Straightforward.
def method(self, First):
First() #instantiation
First.classmethod()
First.staticmethod()
In python classes are objects itself, so you are able to call your method
like this
second_instance.method(Any_Class_You_Want)
Upvotes: 3