finding name of instances created

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

    finding name of instances created

    Short version of what I am looking for:

    Given a class "public_cla ss" which is instantiated a few times e.g.

    a = public_class()
    b = public_class()
    c = public_class()

    I would like to find out the name of the instances so that I could
    create a list of them e.g.
    ['a', 'b', 'c']

    I've read the Python Cookbook, Python in a Nutshell, Programming
    Python, Learning Python, ... googled (probably missed something
    obvious), all to no avail.

    =====
    Longer version:

    If I can do the above, I believe I could do the following thing which
    is what I am really after eventually.

    Given the statement
    [color=blue][color=green]
    >> a = public_class()[/color][/color]

    I would like to generate
    [color=blue][color=green]
    >> my_dict['a'] = private_class()[/color][/color]

    so that one could write
    [color=blue][color=green]
    >> a.apparently_si mple_method()[/color][/color]

    and that, behind the scene, I could translate that as
    [color=blue][color=green]
    >> my_dict['a'].not_so_simple_ method()[/color][/color]

    as well as do things like
    [color=blue][color=green]
    >> for name in my_dict:
    >> do_stuff(name)[/color][/color]

    Any help, pointers, sketches or outline of solution would be greatly
    appreciated.

    André

  • Craig Ringer

    #2
    Re: finding name of instances created

    On Fri, 2005-01-21 at 16:13 -0800, André wrote:[color=blue]
    > Short version of what I am looking for:
    >
    > Given a class "public_cla ss" which is instantiated a few times e.g.
    >
    > a = public_class()
    > b = public_class()
    > c = public_class()
    >
    > I would like to find out the name of the instances so that I could
    > create a list of them e.g.
    > ['a', 'b', 'c']
    >
    > I've read the Python Cookbook, Python in a Nutshell, Programming
    > Python, Learning Python, ... googled (probably missed something
    > obvious), all to no avail.[/color]

    Yep. The short answer is that the instances don't have names - they're
    just bound to names in a particular scope. They can be bound to
    different names in the same scope or in other scopes.

    You can get a dictionary for a particular scope using locals() then
    search it to find the key for a given value. That key will be the name
    the object is bound to in that scope.

    In general, you won't want to do that - the need to do so probably
    suggests a design issue in what you're trying to do.
    [color=blue]
    > If I can do the above, I believe I could do the following thing which
    > is what I am really after eventually.
    >
    > Given the statement
    >[color=green][color=darkred]
    > >> a = public_class()[/color][/color]
    >
    > I would like to generate
    >[color=green][color=darkred]
    > >> my_dict['a'] = private_class()[/color][/color]
    >
    > so that one could write
    >[color=green][color=darkred]
    > >> a.apparently_si mple_method()[/color][/color]
    >
    > and that, behind the scene, I could translate that as
    >[color=green][color=darkred]
    > >> my_dict['a'].not_so_simple_ method()[/color][/color][/color]

    I'm not clear as to why you can't do this as part of the class of which
    'a' is an instance.
    [color=blue]
    > as well as do things like
    >[color=green][color=darkred]
    > >> for name in my_dict:
    > >> do_stuff(name)[/color][/color]
    >
    > Any help, pointers, sketches or outline of solution would be greatly
    > appreciated.[/color]

    I'm not really able to grasp what you're trying to do (but others
    might). It wouldn't hurt if you could post a description of what you're
    actually trying to achieve - /why/ you want this - as that can often be
    very helpful both in understanding what you're thinking and in
    suggesting a suitable approach or alternative.

    --
    Craig Ringer

    Comment

    • Steven Bethard

      #3
      Re: finding name of instances created

      André wrote:[color=blue]
      > Given the statement
      >[color=green][color=darkred]
      >>>a = public_class()[/color][/color]
      >
      > I would like to generate
      >[color=green][color=darkred]
      >>>my_dict['a'] = private_class()[/color][/color]
      >
      > so that one could write
      >[color=green][color=darkred]
      >>>a.apparently _simple_method( )[/color][/color]
      >
      > and that, behind the scene, I could translate that as
      >[color=green][color=darkred]
      >>>my_dict['a'].not_so_simple_ method()[/color][/color]
      >
      > as well as do things like
      >[color=green][color=darkred]
      >>>for name in my_dict:
      >>> do_stuff(name)[/color][/color][/color]

      Why can't you just make public_class a factory, alias the method in
      PrivateClass and access the names through locals()?

      py> class PrivateClass(ob ject):
      .... def not_so_simple_m ethod(self):
      .... print "not so simple"
      .... apparently_simp le_method = not_so_simple_m ethod
      ....
      py> def public_class():
      .... return PrivateClass()
      ....
      py> a = public_class()
      py> a.apparently_si mple_method()
      not so simple
      py> # add 'name' and 'value' to locals() before iteration starts
      py> name, value = None, None
      py> for name, value in locals().iterit ems():
      .... if isinstance(valu e, PrivateClass):
      .... print name, value
      ....
      a <__main__.Priva teClass object at 0x01146D50>

      Steve

      Comment

      • André Roberge

        #4
        Re: finding name of instances created

        Craig Ringer wrote:[color=blue]
        > On Fri, 2005-01-21 at 16:13 -0800, André wrote:
        >[color=green]
        >>Short version of what I am looking for:
        >>
        >>Given a class "public_cla ss" which is instantiated a few times e.g.
        >>
        >>a = public_class()
        >>b = public_class()
        >>c = public_class()
        >>
        >>I would like to find out the name of the instances so that I could
        >>create a list of them e.g.
        >>['a', 'b', 'c'][/color][/color]
        [snip][color=blue]
        >
        >
        > I'm not really able to grasp what you're trying to do (but others
        > might). It wouldn't hurt if you could post a description of what you're
        > actually trying to achieve - /why/ you want this - as that can often be
        > very helpful both in understanding what you're thinking and in
        > suggesting a suitable approach or alternative.
        >[/color]

        Ok, here it goes... I am designing a "learning environment" for Python.
        (See rur-ple.sourceforge .org for details of a *very early, still buggy*
        relase). I have a "world" in which a
        "robot" can accomplish four built-in instructions: move(), turn_left(),
        pick_beeper(), put_beeper().
        turn_left() corresponds to a 90 degree left turn. One can define a
        function to simulate a 90 degree right turn as follows:

        def turn_right():
        turn_left()
        turn_left()
        turn_left()

        and call it as a built-in instruction thereafter.

        By giving more and more complicated tasks for the robot to accomplish,
        one can learn various programming concepts using python syntax:
        def (as above), while, if, else, elif, ......

        I have all of that working well so far (not on sourceforge yet).
        Next, I want to introduce
        the concept of classes and objects, again using python's syntax.

        Behind the scene, I have something like:
        robot_dict = { 'robot' = CreateRobot( ..., name = 'robot') }
        and have mapped move() to correspond to
        robot_dict['robot'].move()
        (which does lots of stuff behind the scene.)

        I have tested robot_dict[] with more than one robot (each with
        its own unique name) and am now at the point where I would like
        to have the ability to interpret something like:

        alex = CreateRobot()
        anna = CreateRobot()

        alex.move()
        anna.move()

        etc. Since I want the user to learn Python's syntax, I don't
        want to require him/her to write
        alex = CreateRobot(nam e = 'alex')
        to then be able to do
        alex.move()

        I have tried various things at the interpreter, found that
        to a class 'a', I could see the instance 'b' created in
        locals():
        'a': <class '__main__.a'>, 'b': <__main__.a object at 0x011515D0>
        which tells me that there must be a way to catch b's name as it is
        created, and do what I want to do.

        Does this clarify what I am trying to do and why?

        André

        Comment

        • André

          #5
          Re: finding name of instances created

          Using the method suggested by Steven Bethard, I *almost* got it working
          the way I would like.
          Here's my program:
          ===
          ..class PrivateClass(ob ject):
          .. dict = {}
          .. def not_so_simple_m ethod(self):
          .. for name in PrivateClass.di ct.keys():
          .. if PrivateClass.di ct[name] == self:
          .. print "instance " + name + " called not so simple"
          .. apparently_simp le_method = not_so_simple_m ethod

          .. def __init__(self):
          .. print "instance created"
          .. for name, value in globals().iteri tems():
          .. if isinstance(valu e, PrivateClass):
          .. PrivateClass.di ct[name] = value

          ..def public_class():
          .. return PrivateClass()

          ..print "=== start==="
          ..alpha = public_class()
          ..print "created alpha"
          ..print PrivateClass.di ct
          ..print "### alpha is not there\n"

          ..beta = public_class()
          ..print "created beta"
          ..print PrivateClass.di ct
          ..print "### we are always one behind in the dict content\n"

          ..alpha.apparen tly_simple_meth od()
          ..beta.apparent ly_simple_metho d()
          =============== =============== ===
          The output follows:
          === start===
          instance created
          created alpha
          {}
          ### alpha is not there

          instance created
          created beta
          {'alpha': <__main__.Priva teClass object at 0x0117CDD0>}
          ### we are always one behind in the dict content

          instance alpha called not so simple
          =======
          Note that instance beta was never recognized when it called "apparently
          simple method".

          I'm sure there must be a way to do this....

          André

          Comment

          • Steven Bethard

            #6
            Re: finding name of instances created

            André wrote:[color=blue]
            > Using the method suggested by Steven Bethard, I *almost* got it working
            > the way I would like.
            > Here's my program:
            > ===
            > .class PrivateClass(ob ject):
            > . dict = {}
            > . def not_so_simple_m ethod(self):
            > . for name in PrivateClass.di ct.keys():
            > . if PrivateClass.di ct[name] == self:
            > . print "instance " + name + " called not so simple"
            > . apparently_simp le_method = not_so_simple_m ethod
            >
            > . def __init__(self):
            > . print "instance created"
            > . for name, value in globals().iteri tems():
            > . if isinstance(valu e, PrivateClass):
            > . PrivateClass.di ct[name] = value
            >
            > .def public_class():
            > . return PrivateClass()
            >
            > .print "=== start==="
            > .alpha = public_class()
            > .print "created alpha"
            > .print PrivateClass.di ct
            > .print "### alpha is not there\n"
            >
            > .beta = public_class()
            > .print "created beta"
            > .print PrivateClass.di ct
            > .print "### we are always one behind in the dict content\n"
            >
            > .alpha.apparent ly_simple_metho d()
            > .beta.apparentl y_simple_method ()[/color]

            It looks like you want PrivateClass.di ct updated every time that
            globals() is updated. You can just use globals directly instead:

            py> class PrivateClass(ob ject):
            .... def __init__(self, globals):
            .... self.globals = globals
            .... def apparently_simp le_method(self) :
            .... for name, value in self.globals.it eritems():
            .... if value is self:
            .... print "instance %s called not so simple" % name
            ....
            py> def public_class():
            .... return PrivateClass(gl obals())
            ....
            py> alpha = public_class()
            py> alpha.apparentl y_simple_method ()
            instance alpha called not so simple
            py> beta = public_class()
            py> beta.apparently _simple_method( )
            instance beta called not so simple

            On the other hand, the iteration in
            PrivateClass.ap parently_simple _method has a very bad code smell...

            Steve

            Comment

            • André

              #7
              Re: finding name of instances created


              Steven Bethard wrote:[color=blue]
              > André wrote:[color=green]
              > > Using the method suggested by Steven Bethard, I *almost* got it[/color][/color]
              working[color=blue][color=green]
              > > the way I would like.[/color][/color]
              [snip][color=blue]
              >
              > It looks like you want PrivateClass.di ct updated every time that
              > globals() is updated.[/color]

              yes, that is what I would like to do.
              [color=blue]
              >You can just use globals directly instead:
              >
              > py> class PrivateClass(ob ject):
              > ... def __init__(self, globals):
              > ... self.globals = globals
              > ... def apparently_simp le_method(self) :
              > ... for name, value in self.globals.it eritems():
              > ... if value is self:
              > ... print "instance %s called not so simple" % name
              > ...
              > py> def public_class():
              > ... return PrivateClass(gl obals())
              > ...
              > py> alpha = public_class()
              > py> alpha.apparentl y_simple_method ()
              > instance alpha called not so simple
              > py> beta = public_class()
              > py> beta.apparently _simple_method( )
              > instance beta called not so simple
              >[/color]
              That's exactly what I was looking for; thank you!
              [color=blue]
              > On the other hand, the iteration in
              > PrivateClass.ap parently_simple _method has a very bad code smell...
              >[/color]
              I'm not sure what you mean...
              Is it because it makes use of information that is
              exterior to the class, which is not passed as a parameter
              to the method?
              [feel free to ignore this question if you want; you have
              already helped me tremendously!!!]

              André

              Comment

              • Steven Bethard

                #8
                Re: finding name of instances created

                André Roberge wrote:[color=blue]
                > Behind the scene, I have something like:
                > robot_dict = { 'robot' = CreateRobot( ..., name = 'robot') }
                > and have mapped move() to correspond to
                > robot_dict['robot'].move()
                > (which does lots of stuff behind the scene.)
                >
                > I have tested robot_dict[] with more than one robot (each with
                > its own unique name) and am now at the point where I would like
                > to have the ability to interpret something like:
                >
                > alex = CreateRobot()
                > anna = CreateRobot()
                >
                > alex.move()
                > anna.move()
                >
                > etc. Since I want the user to learn Python's syntax, I don't
                > want to require him/her to write
                > alex = CreateRobot(nam e = 'alex')
                > to then be able to do
                > alex.move()[/color]

                How do you get the commands from the user? Maybe you can preprocess the
                user code?

                py> class Robot(object):
                .... def __init__(self, name):
                .... self.name = name
                .... def move(self):
                .... print "robot %r moved" % self.name
                ....
                py> user_code = """\
                .... alex = Robot()
                .... anna = Robot()
                .... alex.move()
                .... anna.move()"""
                py> new_user_code = re.sub(r'(\w+)\ s+=\s+Robot\(\) ',
                .... r'\1 = Robot(name="\1" )',
                .... user_code)
                py> print new_user_code
                alex = Robot(name="ale x")
                anna = Robot(name="ann a")
                alex.move()
                anna.move()
                py> exec new_user_code
                robot 'alex' moved
                robot 'anna' moved

                Steve

                Comment

                • Steven Bethard

                  #9
                  Re: finding name of instances created

                  André wrote:[color=blue]
                  > Steven Bethard wrote:
                  >[color=green]
                  >>André wrote:
                  >>[color=darkred]
                  >>>Using the method suggested by Steven Bethard, I *almost* got it[/color][/color]
                  >
                  > working
                  >[color=green][color=darkred]
                  >>>the way I would like.[/color][/color]
                  >
                  > [snip]
                  >[color=green]
                  >>It looks like you want PrivateClass.di ct updated every time that
                  >>globals() is updated.[/color]
                  >
                  >
                  > yes, that is what I would like to do.
                  >
                  >[color=green]
                  >>You can just use globals directly instead:
                  >>
                  >>py> class PrivateClass(ob ject):
                  >>... def __init__(self, globals):
                  >>... self.globals = globals
                  >>... def apparently_simp le_method(self) :
                  >>... for name, value in self.globals.it eritems():
                  >>... if value is self:
                  >>... print "instance %s called not so simple" % name
                  >>...
                  >>py> def public_class():
                  >>... return PrivateClass(gl obals())
                  >>...
                  >>py> alpha = public_class()
                  >>py> alpha.apparentl y_simple_method ()
                  >>instance alpha called not so simple
                  >>py> beta = public_class()
                  >>py> beta.apparently _simple_method( )
                  >>instance beta called not so simple
                  >>[/color]
                  >
                  > That's exactly what I was looking for; thank you!
                  >
                  >[color=green]
                  >>On the other hand, the iteration in
                  >>PrivateClass. apparently_simp le_method has a very bad code smell...
                  >>[/color]
                  >
                  > I'm not sure what you mean...
                  > Is it because it makes use of information that is
                  > exterior to the class, which is not passed as a parameter
                  > to the method?
                  > [feel free to ignore this question if you want; you have
                  > already helped me tremendously!!!][/color]

                  There's a couple things I don't like about it:

                  (1) Generally, I don't like passing globals() around. It's probably
                  okay in this scenario though because if you want to have the user code
                  in a different module, you can do something like:
                  def public_class():
                  return PrivateClass(us ermodule.__dict __)

                  (2) The really bad code smell however is having to iterate through all
                  the values in globals to find the key you're looking for... Especially
                  when you have to check the type of each item... In a perfect world, you
                  would already have a PrivateClass->name mapping somewhere and this
                  should be a simple dict lookup instead of an iterative search... See my
                  other post about potentially pre-processing your user input to see how
                  you can convert the iteration to a simple attribute lookup.

                  Steve

                  Comment

                  • André

                    #10
                    Re: finding name of instances created


                    Steven Bethard wrote:[color=blue]
                    > André Roberge wrote:[color=green]
                    > > Behind the scene, I have something like:
                    > > robot_dict = { 'robot' = CreateRobot( ..., name = 'robot') }
                    > > and have mapped move() to correspond to
                    > > robot_dict['robot'].move()
                    > > (which does lots of stuff behind the scene.)
                    > >
                    > > I have tested robot_dict[] with more than one robot (each with
                    > > its own unique name) and am now at the point where I would like
                    > > to have the ability to interpret something like:
                    > >
                    > > alex = CreateRobot()
                    > > anna = CreateRobot()
                    > >
                    > > alex.move()
                    > > anna.move()
                    > >
                    > > etc. Since I want the user to learn Python's syntax, I don't
                    > > want to require him/her to write
                    > > alex = CreateRobot(nam e = 'alex')
                    > > to then be able to do
                    > > alex.move()[/color]
                    >
                    > How do you get the commands from the user? Maybe you can preprocess[/color]
                    the[color=blue]
                    > user code?
                    >
                    > py> class Robot(object):
                    > ... def __init__(self, name):
                    > ... self.name = name
                    > ... def move(self):
                    > ... print "robot %r moved" % self.name
                    > ...
                    > py> user_code = """\
                    > ... alex = Robot()
                    > ... anna = Robot()
                    > ... alex.move()
                    > ... anna.move()"""
                    > py> new_user_code = re.sub(r'(\w+)\ s+=\s+Robot\(\) ',
                    > ... r'\1 = Robot(name="\1" )',
                    > ... user_code)
                    > py> print new_user_code
                    > alex = Robot(name="ale x")
                    > anna = Robot(name="ann a")
                    > alex.move()
                    > anna.move()
                    > py> exec new_user_code
                    > robot 'alex' moved
                    > robot 'anna' moved
                    >[/color]

                    Smack! (sound of hand slapping forehead).
                    Of course! This is *much* better.
                    (In all honesty, I have trouble reading regular expression
                    notation but I can decode it enough to understand that I can
                    do this - and I already asked a question today on the list
                    about regular expressions, so I have not excuse for not having
                    thought of an approach like this.)

                    I will be already 'processing' the code to make sure that
                    statements/words like: import, exec, eval, input, raw_input, vars,
                    chr, .... are not allowed in the user-defined instructions.
                    This will be just a simple addition.

                    Once again, thank you! Both for this, and for the other example which
                    taught me something about the use of locals(), globals(), and functions
                    that return classes. There are so many corners of Python to explore
                    :-)
                    [color=blue]
                    > Steve[/color]

                    Comment

                    • Steven Bethard

                      #11
                      Re: finding name of instances created

                      André Roberge wrote:[color=blue]
                      > Behind the scene, I have something like:
                      > robot_dict = { 'robot' = CreateRobot( ..., name = 'robot') }
                      > and have mapped move() to correspond to
                      > robot_dict['robot'].move()
                      > (which does lots of stuff behind the scene.)
                      >
                      > I have tested robot_dict[] with more than one robot (each with
                      > its own unique name) and am now at the point where I would like
                      > to have the ability to interpret something like:
                      >
                      > alex = CreateRobot()
                      > anna = CreateRobot()
                      >
                      > alex.move()
                      > anna.move()
                      >
                      > etc. Since I want the user to learn Python's syntax, I don't
                      > want to require him/her to write
                      > alex = CreateRobot(nam e = 'alex')
                      > to then be able to do
                      > alex.move()[/color]

                      If you have access to the user module's text, something like this might
                      be a nicer solution:

                      py> class Robot(object):
                      .... def __init__(self):
                      .... self.name = None
                      .... def move(self):
                      .... print "robot %r moved" % self.name
                      ....
                      py> class RobotDict(dict) :
                      .... def __setitem__(sel f, name, value):
                      .... if isinstance(valu e, Robot):
                      .... value.name = name
                      .... super(RobotDict , self).__setitem __(name, value)
                      ....
                      py> user_code = """\
                      .... alex = Robot()
                      .... anna = Robot()
                      .... alex.move()
                      .... anna.move()"""
                      py> robot_dict = RobotDict()
                      py> robot_dict['Robot'] = Robot
                      py> exec user_code in robot_dict
                      robot 'alex' moved
                      robot 'anna' moved

                      Note that I provide a specialized dict in which to exec the user code --
                      this allows me to override __setitem__ to add the appropriate attribute
                      to the Robot as necessary.

                      Steve

                      Comment

                      • Jeremy Bowers

                        #12
                        Re: finding name of instances created

                        On Fri, 21 Jan 2005 21:01:00 -0400, André Roberge wrote:[color=blue]
                        > etc. Since I want the user to learn Python's syntax, I don't want to
                        > require him/her to write
                        > alex = CreateRobot(nam e = 'alex')
                        > to then be able to do
                        > alex.move()[/color]

                        This is just my opinion, but I've been involved with teaching new
                        programmers before so I think it is an informed one. I don't think you
                        teach a language by hiding how the language works, no matter what the
                        intentions.

                        You should be *minimizing* the magic. You're going to train your students
                        that Python objects have names (they don't) and that's going to mess them
                        up later. Actually, it's going to mess them up almost right away, because
                        how can they have a list of robots like this:

                        for robot in robots:
                        robot.turn_left ()

                        That can't work, right, the command inside the loop can only affect the
                        robot named "robot", right?

                        You can't teach Python if what you're actually teaching them is a variant
                        that you have created that is used nowhere else on Earth, and is
                        internally inconsistent to boot (see loop above, the *real* Python
                        variable semantics conflict with the semantics you are teaching).

                        Considering that not a month goes by where someone doesn't post a question
                        related to this, and it has been a FAQ entry for as long as I've used
                        Python, I think you are doing a major disservice to your "users" by
                        training them that objects magically gets the name when assigned. I
                        strongly urge you to do absolutely no pre-processing of any kind to the
                        programs they generate. (By which I mean changes to the code; running
                        pychecker on it would be OK; and I'd urge you to resist the temptation to
                        process its output, either. Provide a "translatio n table" if you need to,
                        but they need to learn to read the real output, too.)

                        Programming is hard enough with burdening people with "pleasant
                        falsehoods". Trust your students to handle the truth (and of course
                        rationally minimize the truth they have to handle, and by using Python
                        you're off to a great start there). If they can't handle the truth, with
                        time, effort, and support, they *sure* as hell can't handle lies!

                        Comment

                        • Alex Martelli

                          #13
                          Re: finding name of instances created

                          André Roberge <andre.roberge@ gmail.com> wrote:
                          [color=blue]
                          > alex = CreateRobot()
                          > anna = CreateRobot()
                          >
                          > alex.move()
                          > anna.move()[/color]

                          Hmmmm -- while I've long since been identified as a 'bot, I can assure
                          you that my wife Anna isn't!


                          Alex

                          Comment

                          • André

                            #14
                            Re: finding name of instances created


                            Jeremy Bowers wrote:[color=blue]
                            > On Fri, 21 Jan 2005 21:01:00 -0400, André Roberge wrote:[color=green]
                            > > etc. Since I want the user to learn Python's syntax, I don't want[/color][/color]
                            to[color=blue][color=green]
                            > > require him/her to write
                            > > alex = CreateRobot(nam e = 'alex')
                            > > to then be able to do
                            > > alex.move()[/color]
                            >
                            > This is just my opinion, but I've been involved with teaching new
                            > programmers before so I think it is an informed one. I don't think[/color]
                            you[color=blue]
                            > teach a language by hiding how the language works, no matter what the
                            > intentions.
                            >
                            > You should be *minimizing* the magic. You're going to train your[/color]
                            students[color=blue]
                            > that Python objects have names (they don't) and that's going to mess[/color]
                            them[color=blue]
                            > up later. Actually, it's going to mess them up almost right away,[/color]
                            because[color=blue]
                            > how can they have a list of robots like this:
                            >
                            > for robot in robots:
                            > robot.turn_left ()[/color]

                            They will not be able to do that.
                            [color=blue]
                            >
                            > That can't work, right, the command inside the loop can only affect[/color]
                            the[color=blue]
                            > robot named "robot", right?
                            >
                            > You can't teach Python if what you're actually teaching them is a[/color]
                            variant[color=blue]
                            > that you have created that is used nowhere else on Earth, and is
                            > internally inconsistent to boot (see loop above, the *real* Python
                            > variable semantics conflict with the semantics you are teaching).
                            >[/color]

                            I think you misunderstood my intentions, possibly because I explain
                            things too superficially.
                            [color=blue]
                            > Considering that not a month goes by where someone doesn't post a[/color]
                            question[color=blue]
                            > related to this, and it has been a FAQ entry for as long as I've used
                            > Python, I think you are doing a major disservice to your "users" by
                            > training them that objects magically gets the name when assigned. I
                            > strongly urge you to do absolutely no pre-processing of any kind to[/color]
                            the[color=blue]
                            > programs they generate. (By which I mean changes to the code; running
                            > pychecker on it would be OK; and I'd urge you to resist the[/color]
                            temptation to[color=blue]
                            > process its output, either. Provide a "translatio n table" if you need[/color]
                            to,[color=blue]
                            > but they need to learn to read the real output, too.)
                            >
                            > Programming is hard enough with burdening people with "pleasant
                            > falsehoods". Trust your students to handle the truth (and of course
                            > rationally minimize the truth they have to handle, and by using[/color]
                            Python[color=blue]
                            > you're off to a great start there). If they can't handle the truth,[/color]
                            with[color=blue]
                            > time, effort, and support, they *sure* as hell can't handle lies![/color]

                            The environment in which students (my kids first, others later :-) will
                            learn is based on Richard Pattis's "Karel the Robot" (adapted for
                            Python in "Guido van Robot"). They are presented with a robot that can
                            do four basic actions, as I described in a previous post. It's been
                            used successfully in many places.

                            The students learn first the procedural aspect of python. Here's a
                            quick example of a program that they can write:

                            ..def move_and_turn() :
                            .. move()
                            .. turn_left()
                            ..
                            ..def draw_square():
                            .. for i in range(4):
                            .. move_and_turn()
                            ..
                            ..draw_square()
                            ======
                            At this point, they don't know anything about objects and methods; but
                            they will have learned about functions and variables. This is where
                            'Guido van Robot (GvR)', which has been used succesfully to teach
                            programming using a syntax somewhat similar to python, but not quite,
                            stops. (Actually, you can't use variables in GvR).

                            I want to move beyond that and introduce objects and classes.

                            So, students will be able to write:
                            pete = CreateRobot(2, 3)
                            pete.move()

                            learning about objects and methods.

                            As for things like
                            for robot in robots:
                            do stuff

                            that will be for my use only: drawing robots on the screen, updating
                            the 'world' when robots pick stuff up, etc. My intention is that the
                            students will use the EXACT python syntax, so that they don't know that
                            *I* have given a *name* to their robot(s) behind the scene.
                            I have to cut this short; I hope it clarifies my intentions.

                            André

                            Comment

                            • Scott David Daniels

                              #15
                              Re: finding name of instances created

                              André Roberge wrote:[color=blue]
                              > Craig Ringer wrote:
                              >[color=green]
                              >> On Fri, 2005-01-21 at 16:13 -0800, André wrote:
                              >>[color=darkred]
                              >>> Short version of what I am looking for:
                              >>>
                              >>> Given a class "public_cla ss" which is instantiated a few times e.g.
                              >>>
                              >>> a = public_class()
                              >>> b = public_class()
                              >>> c = public_class()
                              >>>
                              >>> I would like to find out the name of the instances so that I could
                              >>> create a list of them e.g.
                              >>> ['a', 'b', 'c'][/color][/color]
                              >
                              > ...
                              > Behind the scene, I have something like:
                              > robot_dict = { 'robot' = CreateRobot( ..., name = 'robot') }
                              > and have mapped move() to correspond to robot_dict['robot'].move()
                              > (which does lots of stuff behind the scene.)
                              > ...[good explanation]...
                              > Does this clarify what I am trying to do and why?[/color]

                              Yup. Would something like this help?

                              parts = globals().copy( )
                              parts.update(lo cals())
                              names = [name for name, value in parts.iteritems ()
                              if isinstance(valu e, Robot)] # actual class name here

                              Note, however, that

                              a = b = CreateRobot()

                              will give two different names to the same robot.

                              And even:

                              Karl = CreateRobot()
                              Freidrich = CreateRobot()
                              for robot in (Karl, Freidrich):
                              robot.move()

                              Will have two names for "Freidrich" -- Freidrich and robot


                              --Scott David Daniels
                              Scott.Daniels@A cm.Org

                              Comment

                              Working...