0
#!/usr/bin/env python  
class AA(object):
    def __init__(self):
        pass
    def y(self):
        pass

x=AA()
x.y()

When I execute x.y(), I want to print "This is 'x' call me", how should I do it ?

ettanany
  • 19,038
  • 9
  • 47
  • 63
user1334609
  • 381
  • 4
  • 12

4 Answers4

1

I hope that this will solve your issue

#!/usr/bin/env python


class AA(object):
    def __init__(self):
        pass

    def y(self, name):
        self.name = name
        print("This is %s call me" % name)


x = AA()
x.y("Tarzan")
ricristian
  • 466
  • 4
  • 17
  • I really don't want to pass a parameter to get this, "x" is already there, i think there should be a way to print it out – user1334609 Jul 28 '17 at 09:19
  • @user1334609 no, there's none. The `AA` instance referenced by name `x` knows _nothing_ about `x`. Believe it or not but this is the best answer you'll get. – bruno desthuilliers Jul 28 '17 at 09:26
0

Everything is an object in Python, When you create an instance of the class it allocate memory location and that memory location is referenced by your x variable.The only object has memory location, variable doesn't have any memory location. Variable just refer to objects memory location

in your example, X is nothing just reference to your memory location

if define a variable

a = 2

that means a reference to 2

a = 1

that means a now reference to 1

Assigning one variable to another makes a new tag bound to the same value as shown below.

b = a

that means a and b both reference to 1

id() in python return memory location

print id(a)
print id(b)

output

140621897573617
140621897573617

Example 1:

>>> s1 = 'hello'

>>> s2 = 'hello'

>>> id(s1), id(s2) 
(4454725888, 4454725888)

>>> s1 == s2 True

>>> s1 is s2 True

>>> s3 = 'hello, world!'

>>> s4 = 'hello, world!'

>>> id(s3), id(s4) (4454721608, 4454721664)

>>> s3 == s4 True

>>> s3 is s4 False

Example 2

>>> class Foo:
...     pass
...
>>> bar = Foo()
>>> baz = Foo()
>>> id(bar)
140730612513248
>>> id(baz)
140730612513320

result

Name of object or instance is nothing just reference to memory location

Kallz
  • 3,244
  • 1
  • 20
  • 38
  • no, I just want to now which objects are calling this function, we can some name conventions(vm1 vm2, vm3..) for writing code for example, we have lots of vm instance,vm1=AA(), vm1.run_cmd("xxxx"), vm2=AA(), vm2.run_cmd("") I want to know which vm are run some cmd – user1334609 Jul 28 '17 at 09:11
  • 1
    ID is not readable, I actually want to show this info in the log to make it sense – user1334609 Jul 28 '17 at 12:42
  • @user1334609, do you want to show vm's name in log or you want to show variable's name in log.? I feel vm's name would be more appropriate. – Hara Jul 28 '17 at 12:48
  • @user1334609 you can read memory address by using Ctype module in Python if you want exact name then you have to create instance with id or name – Kallz Jul 28 '17 at 13:00
  • @Haranadh I want to use variable as the vm name, I ask people to use a self-explanation name as variable name, I don't want to pass extra parameter here – user1334609 Jul 28 '17 at 14:07
  • How to use it ? – user1334609 Jul 28 '17 at 14:20
0

From @user1334609 's comment:

for example, we have lots of vm instance,vm1=AA(), vm1.run_cmd("xxxx"), vm2=AA(), vm2.run_cmd("") I want to know which vm are run some cmd

To know which VM has run the command you can just use the id(self), instead of trying to find the declared variable in code.

Two options you have now to see from which vm, command is running.

Option1: Add a member variable to class. This can give readability.

Option2: Use the id of self in y(). This avoids adding additional variable.

Example code:

#!/usr/bin/env python
class AA(object):
    def __init__(self, vmname):
        self.whoami = vmname

    def y(self):
        print "My Name is %s " % self.whoami # Option1
        print "My Id is %s " % id(self) # Option2


def main():
    vm1=AA("Yoda")
    vm1.y()
    vm2=AA("Boda")
    vm2.y()
    vm3=AA("Anakin")
    vm3.y()


if __name__ == '__main__':
    main()

This gives following output:

My Name is Yoda 
My Id is 139725977256656 
My Name is Boda 
My Id is 139725977256720 
My Name is Anakin 
My Id is 139725977256784 
Hara
  • 1,467
  • 4
  • 18
  • 35
  • I really want to avoid using parameters – user1334609 Jul 28 '17 at 12:41
  • @user1334609, If dont want to use parameters, then inside `y(self)` you could check the id of self, which would help you identifying from which `vm` object call happens. Means you could choose option2. – Hara Jul 28 '17 at 12:43
0

I have posted a complete solution here:

https://stackoverflow.com/a/49331683/7386061

It works without parameters. For example you could just do:

class AA(RememberInstanceCreationInfo):
    def y(self):
        print("my name is '"+self.creation_name+"'")

x=AA()
x.y()
out: my name is 'x'
TheoRet
  • 31
  • 4