how to get a class instance name during creation?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dmitrey

    how to get a class instance name during creation?

    hi all,
    I have a code
    z = MyClass(some_ar gs)
    can I somehow get info in MyClass __init__ function that user uses "z"
    as name of the variable?

    I.e. to have __init__ function that creates field z.name with value
    "z".

    Thank you in advance, D.
  • Gabriel Genellina

    #2
    Re: how to get a class instance name during creation?

    En Fri, 03 Oct 2008 15:04:01 -0300, dmitrey <dmitrey.kroshk o@scipy.org>
    escribió:
    I have a code
    z = MyClass(some_ar gs)
    can I somehow get info in MyClass __init__ function that user uses "z"
    as name of the variable?
    >
    I.e. to have __init__ function that creates field z.name with value
    "z".
    No. I'd just write it as:

    z = MyClass(some, args, 'z')

    wich is pretty simple and readable (although you have to repeat the 'z').
    All the alternative ways I know of require too much black magic.

    --
    Gabriel Genellina

    Comment

    • Aaron \Castironpi\ Brady

      #3
      Re: how to get a class instance name during creation?

      On Oct 3, 1:46 pm, Bruno Desthuilliers
      <bdesth.quelque ch...@free.quel quepart.frwrote :
      dmitrey a écrit :
      >
      hi all,
      I have a code
      z = MyClass(some_ar gs)
      can I somehow get info in MyClass __init__ function that user uses "z"
      as name of the variable?
      >
      I.e. to have __init__ function that creates field z.name with value
      "z".
      >
      This has been debated to hell and back. To make a long story short, here
      are two common use cases:
      >
      x = MyClass()
      y = x
      del x
      >
      objects = [MyClass() for i in range(100)]
      >
      If you can come with a meaningfull answer to "what's *the* name of any
      of the MyClass instance(s)" in both cases, then please let us know...
      Hmmm, just thinking:

      What about a function:

      autoname( 'x= MyClass' )

      which calls exec as well as determines the name? That could use
      'code' and 'ast' modules and 'parse', or just take the first \w
      characters, assuming assignment, or:

      autoname( 'x', MyClass() )

      which executes assignment and sets an attr or calls a method on the
      second arg.

      Comment

      • dmitrey

        #4
        Re: how to get a class instance name during creation?

        On Oct 3, 9:46 pm, Bruno Desthuilliers
        <bdesth.quelque ch...@free.quel quepart.frwrote :
        dmitrey a écrit :
        >
        hi all,
        I have a code
        z = MyClass(some_ar gs)
        can I somehow get info in MyClass __init__ function that user uses "z"
        as name of the variable?
        >
        I.e. to have __init__ function that creates field z.name with value
        "z".
        >
        This has been debated to hell and back. To make a long story short, here
        are two common use cases:
        >
        x = MyClass()
        y = x
        del x
        >
        objects = [MyClass() for i in range(100)]
        >
        If you can come with a meaningfull answer to "what's *the* name of any
        of the MyClass instance(s)" in both cases, then please let us know...
        I had seen the examples during google search, still I hoped for an
        answer to my exact situation. I know for sure there will be no
        renaming and creating like the above objects = [MyClass() for i in
        range(100)].
        as for the z = MyClass(some, args, 'z') I had it in my mind but I
        hoped to have something automatic, w/o the arg 'z'.
        Regards, D.

        Comment

        • Ben Finney

          #5
          Re: how to get a class instance name during creation?

          dmitrey <dmitrey.kroshk o@scipy.orgwrit es:
          On Oct 3, 9:46 pm, Bruno Desthuilliers
          <bdesth.quelque ch...@free.quel quepart.frwrote :
          x = MyClass()
          y = x
          del x

          objects = [MyClass() for i in range(100)]

          If you can come with a meaningfull answer to "what's *the* name of
          any of the MyClass instance(s)" in both cases, then please let us
          know...
          >
          I had seen the examples during google search, still I hoped for an
          answer to my exact situation. I know for sure there will be no
          renaming and creating like the above objects = [MyClass() for i in
          range(100)].
          You *know* this, *for sure*? The only way I can think of that would
          give you such certain knowledge that such a situation will not happen
          is an automated, full-coverage unit test suite of all code that uses
          your class. Which is an excellent position to be in, so I commend you
          on your diligence.

          So, why is it that you wish to restrict users of your class to never
          do such normal operations with instances as in the above examples?
          What problem are you solving by this restriction?

          --
          \ “Holy human pressure cookers, Batman!” —Robin |
          `\ |
          _o__) |
          Ben Finney

          Comment

          Working...