一个班级问题 [英] A class question

查看:72
本文介绍了一个班级问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


有没有办法在调试中从一个类中的

访问实例变量名?

例如:

类X:

def debug(self):

print"我的实例var是%s" %(一些神奇的Python东西)


这样:

Hello,

Is there a way I can, for debugging, access the instance variable name from
within a class?
E.g:
Class X:
def debug(self):
print "My instance var is %s" % (some magic Python stuff)

So that:


>>> x = X()
x.debug()
我的实例var是x
>>>x = X()
x.debug()
My Instance var is x



(不传递名称:x = X(名称=" x"))


Thx。

\\ \\ div

( Without passing the name in like: x=X(name="x") )

Thx.
\d

推荐答案

Donn Ingleaécrit:
Donn Ingle a écrit :

您好,


有没有办法在调试中从一个类中的

访问实例变量名?

例如:

Class X:

def debug(self):

print" my instance var is%s" %(一些神奇的Python东西)


这样:
Hello,

Is there a way I can, for debugging, access the instance variable name from
within a class?
E.g:
Class X:
def debug(self):
print "My instance var is %s" % (some magic Python stuff)

So that:

>> > x = X()
x.debug()
我的实例var是x
>>>x = X()
x.debug()
My Instance var is x



(不传递名称) in like:x = X(name =" x"))


( Without passing the name in like: x=X(name="x") )



什么应该是变量名称?在以下情况下?


a = b = c = X()

X()。debug()

What should be the "variable name" in the following situations ?

a = b = c = X()
X().debug()


10月28日上午6:01,Donn Ingle< donn.in ... @ gmail.comwrote:
On Oct 28, 6:01 am, Donn Ingle <donn.in...@gmail.comwrote:

你好,


有没有办法在调试中从一个类中的

访问实例变量名?

例如:

Class X:

def debug(self):

print" my instance var is%s" %(一些神奇的Python东西)


这样:
Hello,

Is there a way I can, for debugging, access the instance variable name from
within a class?
E.g:
Class X:
def debug(self):
print "My instance var is %s" % (some magic Python stuff)

So that:

>> x = X()
x.debug()
我的实例var是x
>>x = X()
x.debug()
My Instance var is x



(没有传递名称等:x = X(name =" x"))


( Without passing the name in like: x=X(name="x") )



您可以在调用者的全局和本地名称空间中搜索

与自己绑定在同一个对象上的符号。例如,要在调用者的全局命名空间中找到

a这样的符号,这可能会起作用:


def debug(self):

表示sym,值为sys._getframe(1).f_globals:

如果值为self:

print"我的实例var是%s" %sym


改进示例作为练习。在Python Cookbook中可能会有相当多的食谱来做这样的事情(quod

googla)。


另外,请注意某些对象没有绑定任何符号,而是由列表或其他容器引用



Carl Banks


You could search the global and local namespaces of the caller for a
symbol that''s bound to the same object as self. For instance, to find
a such a symbol in the caller''s global namespace, this might work:

def debug(self):
for sym,value in sys._getframe(1).f_globals:
if value is self:
print "My instance var is %s" % sym

Improving the example left as an exercise. There''s probably quite a
few recipes to do stuff like this in the Python Cookbook (quod
googla).

Also, note that some objects are not bound to any symbols but instead
are referenced by lists or other containers.
Carl Banks


Hrvoje Niksicaécrit:
Hrvoje Niksic a écrit :

Donn Ingle< do ******** @ gmail.comwrites:
Donn Ingle <do********@gmail.comwrites:

>有没有办法在调试中从一个类中的
访问实例变量名?
例如:
X类:
def debug(self):
print"我的实例var是%s" %(一些神奇的Python东西)
>Is there a way I can, for debugging, access the instance variable name from
within a class?
E.g:
Class X:
def debug(self):
print "My instance var is %s" % (some magic Python stuff)



正如其他人已经回答的那样,一个实例可以存在很多变量,


As others have answered, an instance can live in many variables,



与许多名字绑定会更准确恕我直言。 Python''

"变量"是name =>对象绑定。

"be bound to many names" would be more accurate IMHO. Python''s
"variables" are name=>object bindings.


这篇关于一个班级问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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