method to print name of an instance of a class
Question:
I am new to classes and writing one to perform a tracking and timing task. Have looked at this but still having trouble getting one aspect of the functionality to work.
Here’s the part of what I’ve got to demonstrate the problem:
class seperate_trackers():
def __init__(self):
print ("class initiated")
def print_instance_name(self):
print (self.__class__.__name__)
Create an instance of it:
track_task1 = separate_trackers()
>> class initiated
Run the method in there:
track_task1.print_instance_name()
>> separate_trackers
That’s not what I want!
How can that method be fixed so it returns track_task1
when it is run?
Answers:
This is not a good idea. If you want your instance to have a name, that should be an attribute of the instance itself (the name of the variabe is just a pointer and it should not represent the object’s state).
Try this instead:
# We don't usually use snake case for class names in python (and its 'separate')
class SeparateTrackers():
def __init__(self, name):
self.name = name
instance1 = SeparateTrackers("instance_name")
print(instance1.name) # instance_name
Objects don’t know what variables refer to them. There can be any number of references to an object, and none of them is “the real one,” they are all equally valid as names for the object. Furthermore, there may be no references that are simple names:
things = [1, "hello", separate_trackers(), 3.14]
There’s no useful way to find out what variables refer to an object.
class SeparateTrackers:
def __init__(self, instance_name):
self.instance_name = instance_name
def __str__(self):
return self.instance_name
So you can use something like
a = SeparateTracker("first instance")
print(a) # print instance's name
Probably not something you’d want to rely on, but this is what I have if you’d like to mess around with it. Tested using python 3.11.0.
from inspect import currentframe, getframeinfo
from math import pi
class Circle:
def __init__(self, radius):
self.radius = radius
def __call__(self):
print(f'{self.get_caller_name()} called itself.')
def area(self):
print(f'{self.get_caller_name()} called area().')
return pi * self.radius ** 2
def get_caller_name(self):
caller_frame = currentframe().f_back.f_back
caller_locals = caller_frame.f_locals
for name, obj in caller_locals.items():
if obj is self:
return name
print("Could not determine the name referencing this instance.")
}
c1 = Circle(2)
c1()
c1.area()
Outputs:
> c1 called itself.
> c1 called area().
I am new to classes and writing one to perform a tracking and timing task. Have looked at this but still having trouble getting one aspect of the functionality to work.
Here’s the part of what I’ve got to demonstrate the problem:
class seperate_trackers():
def __init__(self):
print ("class initiated")
def print_instance_name(self):
print (self.__class__.__name__)
Create an instance of it:
track_task1 = separate_trackers()
>> class initiated
Run the method in there:
track_task1.print_instance_name()
>> separate_trackers
That’s not what I want!
How can that method be fixed so it returns track_task1
when it is run?
This is not a good idea. If you want your instance to have a name, that should be an attribute of the instance itself (the name of the variabe is just a pointer and it should not represent the object’s state).
Try this instead:
# We don't usually use snake case for class names in python (and its 'separate')
class SeparateTrackers():
def __init__(self, name):
self.name = name
instance1 = SeparateTrackers("instance_name")
print(instance1.name) # instance_name
Objects don’t know what variables refer to them. There can be any number of references to an object, and none of them is “the real one,” they are all equally valid as names for the object. Furthermore, there may be no references that are simple names:
things = [1, "hello", separate_trackers(), 3.14]
There’s no useful way to find out what variables refer to an object.
class SeparateTrackers:
def __init__(self, instance_name):
self.instance_name = instance_name
def __str__(self):
return self.instance_name
So you can use something like
a = SeparateTracker("first instance")
print(a) # print instance's name
Probably not something you’d want to rely on, but this is what I have if you’d like to mess around with it. Tested using python 3.11.0.
from inspect import currentframe, getframeinfo
from math import pi
class Circle:
def __init__(self, radius):
self.radius = radius
def __call__(self):
print(f'{self.get_caller_name()} called itself.')
def area(self):
print(f'{self.get_caller_name()} called area().')
return pi * self.radius ** 2
def get_caller_name(self):
caller_frame = currentframe().f_back.f_back
caller_locals = caller_frame.f_locals
for name, obj in caller_locals.items():
if obj is self:
return name
print("Could not determine the name referencing this instance.")
}
c1 = Circle(2)
c1()
c1.area()
Outputs:
> c1 called itself.
> c1 called area().