How to get the name of the object being passed to a function within the function?

Solution for How to get the name of the object being passed to a function within the function?
is Given Below:

I’m taking machine learning and see myself peeking at the data before building models and so on. I want to have a function to give me the top N sample of the data.
Let say I have function sample_data defined like this:

"""print top N of the data
"""
def sample_data (data_name, n):
    print ('{}: {}'.format(name_of_the_data_being_passed_to_parameter_data_name, data_name[:n]))

I have tried

eval()

but it didn’t work.

I also tried

.__code__.co_varnames

but it returns the parameter names of the function itself, NOT the object being passed.

Is there a way to achieve this? I’m using python 2.7 to match with the class environment.

btw, the object I want to view is pickle file .pkl

TL;DR: It depends on the type of object.

When you do something like x=1, x is a simple type that refers to a value that exists at a certain point in memory. It, itself does not hold a reference to its own name.

However, you can get around this by creating functions and classes.
I’m not overly familiar with python2, but at least in python3 you can call .__name__ on functions and classes.

e.g:

class A:
  pass

def b(self):
  pass

A.__name__ would equal 'A' and b.__name__ would equal b.

Another way to create something like this is to create your own type. Again, I’m not sure if this works in python2, but in python3 you can do something like: x = type('a', (), {}) and then x.__name__ would equal 'a'.

Edit:
A class object will have a __name__ attribute, but once it has been instantiated, that attribute would no longer exist. To get around this, you would need to call .__class__ before .__name__.