查找创建的实例名称 [英] finding name of instances created

查看:79
本文介绍了查找创建的实例名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找的简短版本:


给定一个类public_class实例化了几次,例如


a = public_class()

b = public_class()

c = public_class()

我想找出实例的名称,这样我就可以创建一个列表,例如

[''a '','''',''c'']


我已经阅读了Python Cookbook,果壳中的Python,编程

Python,学习Python,... googled(可能错过了一些东西

明显),一切都无济于事。


=====

更长版本:


如果我可以做到以上几点,我相信我可以做以下事情

是我最终真正追求的。


鉴于声明

a = public_class()


我想生成

my_dict [''a''] = private_class()


这样就可以写出

a.apparently_simple _method()


,在幕后,我可以翻译为

my_dict [''a'']。not_so_simple_method()


以及在my_dict中为名称做

之类的事情:
do_stuff(name)




任何帮助,指针,草图或解决方案的大纲都会非常好。

$ b $bAndré

解决方案

周五,2005-01-21 16:13 -0800,Andr ??写道:

我正在寻找的简短版本:

给定一个类public_class实例化了几次,例如

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

我想找出实例的名称,以便我可以创建一个列表,例如
['''',''b'','c'']


是的。简短的回答是,实例没有名字 - 他们只是绑定在特定范围内的名字。它们可以绑定到相同范围或其他范围内的不同名称。


您可以使用locals()获取特定范围的字典

搜索它以找到给定值的键。该密钥将是该对象在该范围内绑定的名称




一般来说,你不想这样做 - 需要这样做可能

暗示了你想要做的设计问题。

如果我能做到以上几点,我相信我可以做以下事情
是我最终真正追求的目标。

鉴于声明

a = public_class()
我想生成
my_dict [''''] = private_class()
这样就可以写一个a.apparently_simple_method()
然后,场景,我可以翻译为
my_dict [''a'']。not_so_simple_method()



我不清楚为什么你不能做这是一个类的一部分

'''''是一个实例。

以及为了名字而做
之类的事情。 my_dict:
do_stuff(姓名)



任何帮助,指示,草图或解决方案概要都会非常感激。




我真的不能理解你想要做什么(但其他人可能会这样做)。如果你可以发布一个关于你实际想要实现的东西的描述 - /为什么/你想要这个 - 这通常可以是

非常有助于理解你的想法和

建议一个合适的方法或替代方案。


-

Craig Ringer


André写道:

鉴于声明

a = public_class()
我想生成
my_dict [''''] = private_class()
以便人们可以写一个
.apparently_simple_method()
然后,在幕后,我可以把它翻译为
my_dict [''a'']。not_so_simple_method()
以及做
之类的事情。 my_dict中的名字:
do_stuff(name)




为什么你不能把public_class变成工厂,别名
PrivateClass并通过locals()访问名称?


py> class PrivateClass(object):

.... def not_so_simple_method(self):

.... print" not so simple"

.... apparently_simple_method = not_so_simple_method

....

py> def public_class():

....返回PrivateClass()

....

py> a = public_class()

py> a.apparently_simple_method()

不那么简单

py> #在迭代开始之前将''name''和''value''添加到locals()

py>名称,价值=无,无

py> for name,value in locals()。iteritems():

.... if isinstance(value,PrivateClass):

.... print name,value

....

a< __ main __。PrivateClass对象位于0x01146D50>


Steve


Craig Ringer写道:

周五,2005-01-21 16:13 -0800,Andr ??写道:

我正在寻找的简短版本:

给定一个类public_class实例化了几次,例如

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

我想找出实例的名称,以便我可以创建一个列表,例如
['''',''b'',''c'']


[snip]

我真的无法理解你想要做什么(但其他人可能)。如果你可以发布你实际想要实现的内容的描述 - /为什么/你想要这个 - 这通常对你理解你的内容非常有帮助,这不会有什么害处''思考并建议采用合适的方法或替代方案。




好​​的,这就是......我正在设计一个学习环境。对于Python。

(请参阅rur-ple.sourceforge.org了解*非常早,但仍有错误*

的详细信息)。我有一个世界其中一个

机器人可以完成四个内置指令:move(),turn_left(),

pick_beeper(),put_beeper()。

turn_left()对应左转90度。可以定义一个

函数来模拟右转90度,如下所示:


def turn_right():

turn_left( )

turn_left()

turn_left()


然后将其称为内置指令。


通过为机器人完成越来越复杂的任务,

可以使用python语法学习各种编程概念:

def(as以上,如果,其他,elif,......


到目前为止,我的所有工作都很顺利(还没有在sourceforge上)。

接下来,我想介绍

类和对象的概念,再次使用python'的语法。


在幕后,我有类似于:

robot_dict = {''机器人''= CreateRobot(...,name =''机器人'')}

并将move()映射到对应于

robot_dict [''机器人'']。移动()

(在场景背后有很多东西。)


我用多个机器人测试了robot_dict [](每个机器人都有

它自己唯一的名字),现在我想要获得
能够解释如下:


alex = CreateRobot()

anna = CreateRobot()


alex。移动()

anna.move()


等等因为我希望用户学习Python的语法,所以我不会/>
想要求他/她写一下

alex = CreateRobot(name =''alex'')

然后才能做到

alex.move()


我在翻译时尝试过各种各样的东西,发现

到一个班级''a'' ,我可以在

locals()中看到实例'b'':

''''':< class''__ main __。a''> ;,''b'':< __ main __。一个对象位于0x011515D0>

告诉我必须有办法捕捉b'的名字,因为它是

创建,并做我想做的事情o。


这是否澄清了我想要做什么以及为什么?


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 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

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:
do_stuff(name)



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

André

解决方案

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
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.
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.
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

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()


I''m not clear as to why you can''t do this as part of the class of which
''a'' is an instance.

as well as do things like
for name in my_dict:
do_stuff(name)



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



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


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:
do_stuff(name)



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

py> class PrivateClass(object):
.... def not_so_simple_method(self):
.... print "not so simple"
.... apparently_simple_method = not_so_simple_method
....
py> def public_class():
.... return PrivateClass()
....
py> a = public_class()
py> a.apparently_simple_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().iteritems():
.... if isinstance(value, PrivateClass):
.... print name, value
....
a <__main__.PrivateClass object at 0x01146D50>

Steve


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
create a list of them e.g.
[''a'', ''b'', ''c'']

[snip]

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.



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(name = ''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??


这篇关于查找创建的实例名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆