0

Firstly, sorry if that name is horribly misleading. I'm not actually sure exactly what I would call what I'm trying to do. By the same token, my searches on the topic may have been lacking, so I apologise if this is something that I really should have been able to look up.

What I'm trying to do is to create a function that uses variables from objects in two separate classes, and then changes a variable for an object in the latter class.

class Type_1:
    def __init__(self, a, b, c, d, e):
        self.variable1 = a
        self.variable2 = b
        self.variable3 = c
        self.variable4 = d
        self.variable5 = e

    def push(target):
        self.variable1 += 5
        target.variable2 -= int(self.variable5 - target.variable3)

class Type_2:
    def __init__(self, a, b, c, d, e):
        self.variable1 = a
        self.variable2 = b
        self.variable3 = c
        self.variable4 = d
        self.variable5 = e

The plan was to find a way to make the push function in Type_1 able to recognise an object in any class, provided it has the necessary variables, as its target and thus affect said object but my way of doing so doesn't work.

Edit: The issue is that when I use the push function, python always specifically looks for variables that belong to a class called target. It doesn't look for a class according to what I entered i.e.

push(John)

would still look to try and try and edit target.variable2 when I'm trying to make it edit John.variable2.

Edit2: This problem has now been solved thanks to mata and Mikhail Karavashkin.

Okinawa Sama
  • 55
  • 1
  • 1
  • 5
  • "doesn't work at all" is a bit unclear. what exactly doesn't work, and how are you trying to use this. That said, your `__init__`s lack a `def` and `push(target)` should probably be `push(self, target)` – mata Jul 13 '15 at 17:23
  • The init issue was just me copying it over poorly. In the version I'm trying to run, it has the def there. Thanks for pointing that out though, I'll change it right away. As for it not working at all, I mean that python looks for the specific variable target.variable2 even if I use the function as push("John") or something. – Okinawa Sama Jul 13 '15 at 17:26
  • I'm not sure in what context you are needing to relate multiple objects together (and I'm not disputing that sometimes you may want this behavior), but I would recommend [this short tutorial](http://www.python-course.eu/python3_properties.php) on getters, setters, and properties as a way to flesh out what you may need. edit: found a [stack-overflow](http://stackoverflow.com/questions/6618002/python-property-versus-getters-and-setters) answer that addresses this as well. – Muttonchop Jul 13 '15 at 17:48

1 Answers1

0

You seem to be pushing to the class, not the instance, in case my corrections to your question are taken into account (def's for __init__ and self for push(self, target) this will work:

>>> a = Type_1(1,2,3,4,5)  # making an instance of Type_1 
>>> b = Type_2(1,2,3,4,5)  # making an instance of Type_2 

>>> a.push(b)   # pushing one to another

>>> b.variable2  # it worked!
0
>>> a.variable1 # it worked!
6
Mikhail Karavashkin
  • 1,365
  • 10
  • 16