0

I try to get the name of the variable, which I passed to a function.

class A():
  def __init__(self):
    self.a = 1

class B():
  def __init__(self):
    self.b = A()
    self.c = A()

  def doSomething(self, hello):
    print(hello)

B().doSomething(B().b)
<__main__.A object at 0x7f67571a3d68>

What I want is that I can identify in the function B().doSomething(), that the variable is b. Is this possible? One restriction is that in the function B().doSomething() only instance variables of B are passed.

For example in peewee (https://github.com/coleifer/peewee), a MySQL ORM in python, they build expressions for filtering like:

B.select().where(B.b == True)

And somehow they are able to identify, that b is passed. Because otherwise the query can not be build properly.

I know they are using static variables in the class, is this maybe the trick?

Thanks for helping! :)

EDNA
  • 139
  • 2
  • 11
  • Question doesn't make sense. Are you trying to determine if `hello == 'b'`? Or are you trying to determine the actual variable name? Or are you trying to determine that `self.b == hello`? – MCBama Dec 09 '17 at 01:10
  • I try to get the actual variable name. it can be too, that I pass B().c for example. Then I would know that c is passed. – EDNA Dec 09 '17 at 01:19

2 Answers2

0

Going by your B().doSomething(B().b) example call I'm going to assume you're attempting to determine if the variable hello is equivalent to the variable b declared on the class B object.

In which case, all you need to do is call the self reference. self refers to the instance of the object that you're working with and every method defined within a class automatically gets reference to the object's self as a method attribute.

Thus, to determine if the the object b variable is equal to the hello parameter all you need to do is if self.b == hello: #do code

MCBama
  • 1,432
  • 10
  • 18
  • If I compare `self.b == hello`, then it could be possible if I pass `B().doSomething(B().c)` that I get the wrong variable name. Because `B().b `and `B().c` have the same class. – EDNA Dec 09 '17 at 01:27
  • No, because `B().b != B().c` because they might be the same class but they're different instances. Thus, they won't be equivalent and you can technically do an `if...else` statement to determine which one was passed. I really don't see why you need to do this though. – MCBama Dec 09 '17 at 01:29
  • Because if I have an query to the database, I must know if the user passed B().b or B().c, for example in B().b I store the price of the house and in B().c I store the phone number of the householder. If I can't this distinguish, then I can not build the expression for the database properly. Could you give me an code snippet, which works? I tried `if self.b == hello:` but it does not work. Maybe because arguments passed by assignment? [link](https://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference) – EDNA Dec 09 '17 at 01:39
0

B().b is not an instance variable of B; rather, it is an instance variable of A. In your constructor in B, you may have meant self.a to be an instance of B or self.a to be an instance of B. If this is your general idea, you can implement a boolean overloading method to destinguish between the two. In the case of your code, it may be best to create a third class, C, to check what class an attribute that is passed to doSomething belongs to:

class A():
   def __init__(self):
     self.a = 1
   def __bool__(self):
        return True

class B():
   def __init__(self):
     self.b = 1
   def __bool__(self):
     return False


class C():
   def __init__(self):
       self.a = A()
       self.b = B()
   def doSomething(self, hello):
       if not hello:
           print("instance of a got passed")
       else:
          print("instance of b got passed")

C().doSomething(C().b)

Output:

instance of b got passed
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
  • Unfortunately not, for example if I have a database with self.a = IntegerField() and self.b = IntegerField(), I use twice the same class. – EDNA Dec 09 '17 at 01:25