Re: finding name of instances created

2005-01-24 Thread Ryan Paul
On Fri, 21 Jan 2005 16:13:19 -0800, André wrote: > Short version of what I am looking for: > > Given a class "public_class" 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 co

Re: finding name of instances created

2005-01-24 Thread André
Ryan Paul wrote: > On Mon, 24 Jan 2005 13:19:45 +, Ryan Paul wrote: > > > > > A working solution: > > > > class A: > > pass > > > > a = A() > > b = A() > > c = A() > > > > [x for x,y in locals().items() if > > hasattr(y,"__class__") and y.__class__ == A] > > > > Just wanted to clarify, bec

Re: finding name of instances created

2005-01-24 Thread Antoon Pardon
Op 2005-01-24, Nick Coghlan schreef <[EMAIL PROTECTED]>: > Steven Bethard wrote: >> That is, you can just keep track of all the names of a Robot in the >> Robot object. In the simple case, where there's only one name, you can >> display it as such. In the more complicated case, where there's so

Re: finding name of instances created

2005-01-24 Thread Ryan Paul
On Mon, 24 Jan 2005 13:19:45 +, Ryan Paul wrote: > > A working solution: > > class A: > pass > > a = A() > b = A() > c = A() > > [x for x,y in locals().items() if > hasattr(y,"__class__") and y.__class__ == A] > Just wanted to clarify, because I know that the intellectually deficient

Re: finding name of instances created

2005-01-24 Thread Fredrik Lundh
Nick Coghlan wrote: > Incidentally, this discussion made me realise the real reason why using a > lambda to create a named > function is evil: > > Py> def f(): pass > ... > Py> f.func_name > 'f' > Py> f = lambda: None > Py> f.func_name > '' > > I think I've heard that explanation before, but it

Re: finding name of instances created

2005-01-24 Thread Nick Coghlan
Steven Bethard wrote: That is, you can just keep track of all the names of a Robot in the Robot object. In the simple case, where there's only one name, you can display it as such. In the more complicated case, where there's some aliasing, you can display the multiple aliases. This means you

Re: finding name of instances created

2005-01-23 Thread Michael Tobis
I have a similar problem. Here's what I do: .def new_robot_named(name,indict=globals()): . execstr = name + " = robot('" + name + "')" . exec(execstr,indict) .class robot(object): . def __init__(self,name): . self.name = name . def sayhi(self): . print "Hi! I'm %s!" % self.nam

Re: finding name of instances created

2005-01-23 Thread Steven Bethard
Nick Coghlan wrote: It also directly addresses the question of aliasing. Think about how Steven's modified dictionary would react to this code: pete = CreateRobot(2, 3) dad = pete dad.move() pete.move() If you'd like to handle these cases, but you don't want to have to explain aliasing right off

Re: finding name of instances created

2005-01-23 Thread Georg Brandl
Michael Tobis wrote: > I have a similar problem. Here's what I do: > > .def new_robot_named(name,indict=globals()): > . execstr = name + " = robot('" + name + "')" > . exec(execstr,indict) > > .class robot(object): > . def __init__(self,name): > . self.name = name > > . def sayhi(se

Re: finding name of instances created

2005-01-23 Thread Steven Bethard
Michael Tobis wrote: I have a similar problem. Here's what I do: .def new_robot_named(name,indict=globals()): . execstr = name + " = robot('" + name + "')" . exec(execstr,indict) .class robot(object): . def __init__(self,name): . self.name = name . def sayhi(self): . print "Hi! I

Re: finding name of instances created

2005-01-23 Thread André
Scott David Daniels wrote: > André Roberge wrote: > > Craig Ringer wrote: > > > >> On Fri, 2005-01-21 at 16:13 -0800, André wrote: > >> > >>> Short version of what I am looking for: > >>> > >>> Given a class "public_class" which is instantiated a few times e.g. > >>> > >>> a = public_class() > >>>

Re: finding name of instances created

2005-01-23 Thread André
Nick Coghlan wrote: > André wrote: > > 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, u

Re: finding name of instances created

2005-01-22 Thread Nick Coghlan
André wrote: 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 i

Re: finding name of instances created

2005-01-22 Thread Nick Coghlan
André wrote: I have tried this exact example (using Python 2.3 if it makes any difference) and what I got was: robot None moved robot None moved I checked what I wrote, used cut & paste on your code, removing the leading "junk", tried it again ... to no avail. :-( Worked exactly as written for me,

Re: finding name of instances created

2005-01-22 Thread André
Steven Bethard wrote: > 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> cl

Re: finding name of instances created

2005-01-22 Thread Scott David Daniels
Andrà Roberge wrote: Craig Ringer wrote: On Fri, 2005-01-21 at 16:13 -0800, Andrà wrote: Short version of what I am looking for: Given a class "public_class" 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 inst

Re: finding name of instances created

2005-01-22 Thread André
Jeremy Bowers wrote: > On Fri, 21 Jan 2005 21:01:00 -0400, André Roberge wrote: > > etc. Since I want the user to learn Python's syntax, I don't want to > > require him/her to write > > alex = CreateRobot(name = 'alex') > > to then be able to do > > alex.move() > > This is just my opinion, but I'v

Re: finding name of instances created

2005-01-22 Thread Alex Martelli
André Roberge <[EMAIL PROTECTED]> wrote: > alex = CreateRobot() > anna = CreateRobot() > > alex.move() > anna.move() H -- while I've long since been identified as a 'bot, I can assure you that my wife Anna isn't! Alex -- http://mail.python.org/mailman/listinfo/python-list

Re: finding name of instances created

2005-01-21 Thread Jeremy Bowers
On Fri, 21 Jan 2005 21:01:00 -0400, Andrà Roberge wrote: > etc. Since I want the user to learn Python's syntax, I don't want to > require him/her to write > alex = CreateRobot(name = 'alex') > to then be able to do > alex.move() This is just my opinion, but I've been involved with teaching new pro

Re: finding name of instances created

2005-01-21 Thread Steven Bethard
Andrà Roberge wrote: 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 i

Re: finding name of instances created

2005-01-21 Thread André
Steven Bethard wrote: > André Roberge wrote: > > 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 test

Re: finding name of instances created

2005-01-21 Thread Steven Bethard
André wrote: Steven Bethard wrote: André wrote: Using the method suggested by Steven Bethard, I *almost* got it working the way I would like. [snip] It looks like you want PrivateClass.dict updated every time that globals() is updated. yes, that is what I would like to do. You can just use global

Re: finding name of instances created

2005-01-21 Thread Steven Bethard
Andrà Roberge wrote: 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 i

Re: finding name of instances created

2005-01-21 Thread André
Steven Bethard wrote: > André wrote: > > Using the method suggested by Steven Bethard, I *almost* got it working > > the way I would like. [snip] > > It looks like you want PrivateClass.dict updated every time that > globals() is updated. yes, that is what I would like to do. >You can just use g

Re: finding name of instances created

2005-01-21 Thread Steven Bethard
André wrote: Using the method suggested by Steven Bethard, I *almost* got it working the way I would like. Here's my program: === .class PrivateClass(object): .dict = {} .def not_so_simple_method(self): .for name in PrivateClass.dict.keys(): .if PrivateClass.dict[name] =

Re: finding name of instances created

2005-01-21 Thread André
Using the method suggested by Steven Bethard, I *almost* got it working the way I would like. Here's my program: === .class PrivateClass(object): .dict = {} .def not_so_simple_method(self): .for name in PrivateClass.dict.keys(): .if PrivateClass.dict[name] == self: .

Re: finding name of instances created

2005-01-21 Thread Andrà Roberge
Craig Ringer wrote: On Fri, 2005-01-21 at 16:13 -0800, Andrà wrote: Short version of what I am looking for: Given a class "public_class" 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

Re: finding name of instances created

2005-01-21 Thread Steven Bethard
André wrote: Given the statement a = public_class() I would like to generate my_dict['a'] = private_class() so that one could write a.apparently_simple_method() and that, behind the scene, I could translate that as my_dict['a'].not_so_simple_method() as well as do things like for name in my_dict:

Re: finding name of instances created

2005-01-21 Thread Craig Ringer
On Fri, 2005-01-21 at 16:13 -0800, Andrà wrote: > Short version of what I am looking for: > > Given a class "public_class" 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 coul

finding name of instances created

2005-01-21 Thread André
Short version of what I am looking for: Given a class "public_class" 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