Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to use variables from 2 instances together inside a method?

Tags:

python

class

I'm sorry if I phrased this wrong, but I will try my best to explain what I want to do.

Is it possible to do this in Python -

class Character():
    strength, skill = 0, 0

    def foo(self, strength, skill):
        if c1.strength > c2.strength:
            #something here

c1 = Character()
c2 = Character()

c1.strength = 15
c2.strength = 13  

I don't really know how to explain this, but what I am trying to do is use the variables from the two instances that I made, inside the method?

Would that code work, or is there anything else? Thanks in advance.

like image 861
user3312175 Avatar asked Dec 07 '25 02:12

user3312175


1 Answers

You could just pass the other instance:

def foo(self, other_character):
    if self.strength > other_character.strength:
        #something here

c1 = Character()
c2 = Character()

c1.strength = 15
c2.strength = 13

c1.foo(c2)
like image 77
tckmn Avatar answered Dec 08 '25 16:12

tckmn