如何将对象的名称作为字符串? [英] How to get an object's name as a string?

查看:88
本文介绍了如何将对象的名称作为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据其他对象名称创建具有算法确定名称的对象

并使用通用算法的对象名称

输入。

如何从对象实例中提取对象的名称为

a string。我认为它存储为

对象的属性,但是连续的''dir()''calles没有找到带有namestring的属性




我的谢谢!

解决方案

Shannon Mayne写道:
< blockquote class =post_quotes>
我想创建具有算法确定名称的对象

基于其他对象名称并使用对象名称进行通用算法

输入。


如何从对象实例中提取对象的名称为

a string。我认为它存储为

对象的属性,但是连续的''dir()''calles没有找到带有namestring的属性




我的谢谢!



再一次(这个主题有很多帖子)。对象可以比Python中的一个名称多得多b
。因此,对象实例和指向它的名称之间没有一对一的对应关系。


示例:


a = myObject()

b = a

c = a


现在a,b, c都指向同一个对象。你如何定义它''的名字'。


你当然可以自由地将一个名称作为属性存储到对象中,但是

联系将是纯逻辑的。


示例:


objects = []

objects.append(myObject (''''))



#查找名称==''a''的对象



obj =无

对象中的对象:

if object.name ==''a'':

obj =对象

-Larry


2008年10月28日上午8:41,Shannon Mayne写道:


我想创建具有算法确定名称的对象

基于其他对象名称并使用对象名称进行通用算法

输入。



名称是什么意思?一个物体?除非你明确定义一个.name属性并指定

它们的名字,否则对象一般都没有.b $ b有名字。


(变量当然有名字,但变量不是一个对象 -

它只是一个对象的引用。许多变量可能指的是

相同的对象,所以要求提供可能是指对象的THE

变量的名称没有任何意义。)


如何从对象实例中提取对象的名称为

a string。我认为它存储为

对象的属性,但是连续的''dir()''calles没有找到带有namestring的属性




如上所述,没有内置的name属性。定义一个,

或许是这样的:

class Foo():

def __init __(姓名):

self.name = name


现在你的Foo对象有一个name属性,如果是x对于这样的对象是一个参考

,你可以将其作为x.name进行访问。


目前还不清楚你打算用什么做这些,但如果在某些

点你想要按名字访问对象(从用户输入或

无论如何),那么你还需要一本字典将名称映射到

对象。所以对于你的__init__函数,你可以添加类似

这样的东西:


name_map [name] = self


其中name_map初始化为文件顶部的{}。那么你

可以使用name_map按名称查找这个类的任何对象。

请记住这会自动保留这些对象

消失当没有其他参考(地图除外)

给他们。如果这是一个问题,当你知道你已经完成它们或者使用弱引用时,明确地将它们从地图中删除




Best ,

- 乔


2008年10月28日星期二09:15:50 -0600,Joe Strout写道:


2008年10月28日上午8:41,Shannon Mayne写道:


>我会喜欢基于其他对象名称创建具有算法确定名称的对象,并使用对象名称进行通用算法输入。



名称是什么意思?一个物体?除非你明确定义一个.name属性并指定

它们的名字,否则对象一般都没有.b $ b有名字。


(变量当然有名字,但变量不是一个对象 - 它只是一个对象的引用。许多变量可能指的是相同的

对象,所以询问变量

的名称是没有任何意义的,这可能是指目前的一个对象。)



这种解释毫无意义。鉴于作业:


x = 57
如果x的名字不是'x''那么
,那么究竟是什么它可能意味着要求变量的名称?


在像Python这样的语言中,术语变量是指变量。是误导和

令人困惑。 Python的编程模型包含对象(值)和名称。

最好使用描述Python实际操作的语言,而不是使用描述其他内容的语言
语言。


-

史蒂文


I would like to create objects with algorithmically determined names
based on other object names and use object names for general algorithm
input.

How would one extract the name of an object from an object instance as
a string. I would think that it is stored as an attribute of the
object but successive ''dir()'' calles haven''t found me the attribute
with the namestring.

My thanks!

解决方案

Shannon Mayne wrote:

I would like to create objects with algorithmically determined names
based on other object names and use object names for general algorithm
input.

How would one extract the name of an object from an object instance as
a string. I would think that it is stored as an attribute of the
object but successive ''dir()'' calles haven''t found me the attribute
with the namestring.

My thanks!

Once again (there have been many posts on this subject). Objects can have more
than one name in Python. Therefore there is not a one-to-one correspondence
between an object instance and name(s) that point to it.

Example:

a = myObject()
b = a
c = a

now a, b, c all point to the same object. How would you define it''s "name".

You are certainly free to store a name as an attribute to the object, but the
linkage would then be purely logical.

Example:

objects = []
objects.append(myObject(''a''))
#
# Find object with name == ''a''
#
obj = None
for object in objects:
if object.name == ''a'':
obj = object
-Larry


On Oct 28, 2008, at 8:41 AM, Shannon Mayne wrote:

I would like to create objects with algorithmically determined names
based on other object names and use object names for general algorithm
input.

What do you mean by the "name" of an object? Objects don''t generally
have names, unless you explicitly define a .name property and assign
them names.

(Variables have names, of course, but a variable isn''t an object --
it''s just a reference to an object. Many variables may refer to the
same object, so it doesn''t make any sense to ask for the name of THE
variable which may be referring to an object at the moment.)

How would one extract the name of an object from an object instance as
a string. I would think that it is stored as an attribute of the
object but successive ''dir()'' calles haven''t found me the attribute
with the namestring.

As noted above, there is no built-in name attribute. Define one,
perhaps like this:

class Foo():
def __init__(name):
self.name = name

Now your Foo objects have a name attribute, and if "x" is a reference
to such an object, you would access that as "x.name".

It''s still unclear what you intend to do with these, but if at some
point you want to access objects by their names (from user input or
whatever), then you''ll also need a dictionary to map names to
objects. So to your __init__ function, you might add something like
this:

name_map[name] = self

where name_map was initialized to {} at the top of the file. Then you
can use name_map to look up any object of this class by name.
Remember that this will keep these objects from automatically
disappearing when there are no other references (other than the map)
to them. If that''s a problem, explicitly remove them from the map
when you know you''re done with them, or use weak references.

Best,
- Joe


On Tue, 28 Oct 2008 09:15:50 -0600, Joe Strout wrote:

On Oct 28, 2008, at 8:41 AM, Shannon Mayne wrote:

>I would like to create objects with algorithmically determined names
based on other object names and use object names for general algorithm
input.


What do you mean by the "name" of an object? Objects don''t generally
have names, unless you explicitly define a .name property and assign
them names.

(Variables have names, of course, but a variable isn''t an object -- it''s
just a reference to an object. Many variables may refer to the same
object, so it doesn''t make any sense to ask for the name of THE variable
which may be referring to an object at the moment.)

That explanation makes no sense. Given the assignment:

x = 57

if the name of x isn''t ''x'', then what on earth can it possibly mean to
ask for the name of a variable?

In languages like Python, the term "variable" is misleading and
confusing. Python''s programming model has objects (values), and names.
Best to use language that describes what Python actually does, rather
than use language that describes what other languages do.


--
Steven


这篇关于如何将对象的名称作为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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