#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Get the name of an instance of a class


    Hi there,

    is it possible to get the name of an instance of a class?

    For example if i do something like that:

    B = car(<Parameters>)

    I want to appear on console:

    "You just created a "car" with the name "B"..."

    The first part is pretty easy i just had to create a variable "type"
    in the class with a strin containing "car" and then do the print command in the __init__ function, but how can i access the name of the actual object? self.__name__ is not working for me.
    Is there a way or is it really impossible? Your help is appreciated

    Regrads,

    Marcurion
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    Nope. "B" is not the name of your instance in any reasonable sense; it's the name of a variable that refers to your instance. You can assign the same instance to different variables, and they will all refer to the same data:

    Code:
    C = B
    D = B
    # Now what's the "name"?  "B", "C", or "D"?  All refer to the same data.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    158
    Rep Power
    3
    You could (if you're in control of the class definition) create an instance attribute, say, "label", which you can pass into the constructor.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481
    self . __class__ . __name__


    >>> (3.14) . __class__ . __name__
    'float'
    >>>
    >>> 3.14.__class__.__name__
    'float'
    Code:
    >>> 2    .__class__.__name__
    'int'
    >>> 2.__class__.__name__
      File "<stdin>", line 1
        2.__class__.__name__
                  ^
    SyntaxError: invalid syntax
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    Okay i assumed it would be impossible due to multiple references, what a shame...
    By the way how can i tab set here in this forum?
    Python code without tab is no fun


    @rrashkin: Did you mean something like that:

    Code:
    class Car:
    
    __init__(self, var1, label)
    self.var1 = var1
    self.label = label
    
    -------------------------------
    
    A = Car(12, "A")
    Guess that would do it for me even it is inconvenient

    @b49P23TIvg:

    Thanks, that was not exactly what i was looking for but it saves me the trouble of using a second string for the class name

    Thank you all for your support

    Regards,
    Marcurion

IMN logo majestic logo threadwatch logo seochat tools logo