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 ?

8
  • Are you looking for the variable name x? should be printed? Commented Jul 28, 2017 at 8:51
  • Just replace pass inside y() with print("This is 'x' call me") Commented Jul 28, 2017 at 8:51
  • 3
    Objects do not and cannot know their names. Commented Jul 28, 2017 at 8:53
  • 1
    I think you're confusing variables with objects. Commented Jul 28, 2017 at 9:05
  • no, I just want to now which objects are calling this function Commented Jul 28, 2017 at 9:10

4 Answers 4

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")
Sign up to request clarification or add additional context in comments.

2 Comments

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 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.
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

6 Comments

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
ID is not readable, I actually want to show this info in the log to make it sense
@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.
@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
@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
|
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 

2 Comments

I really want to avoid using parameters
@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.
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'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.