How to obtain the reference name

Terry Reedy tjreedy at udel.edu
Tue Mar 11 12:54:30 CET 2003


"Paco" <barreraf at inicia.es> wrote in message
news:b4k385$ael$1 at polaris.cc.upv.es...
> That's no the question. The problem is I would make reference to
instance of
> the class, from an attribute of the class.
> This is because I need an attribute that contain python code, and
this code
> need make reference to the instance name.
> That's is:
>
> class A:
>      def __init__(self):
>         self.instance_Name = 'the name I would of the instance'
>         self.code = "print 'The name of the instance is:' +
> self.instance_Name"

Most Python objects do not have names.  They are represented when
printed either by their value (1, 'one', (1,'one'), ['one',1],
{'one':1}) or by type and id <x object at id>.  Python functions,
classes, and modules have a 'definition name' which is part of the
def, class, or import statements.  That definition name is added to
representations so we can keep track of which is which and find the
corresponding code.  That is its only automatic use (that I can think
of at the moment).  It has nothing to do with namespace binding names
except for being the initial bound name.  If you want to do similarly
with  class instances, you must explictly give the instance a name
attribute in the __init__ method (or later).

class A:
  def __init__(self, name)
    self.name = name

a = A('a')

Terry J. Reedy






More information about the Python-list mailing list