Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何访问字符串插值中的实例变量?_Python - Fatal编程技术网

Python 如何访问字符串插值中的实例变量?

Python 如何访问字符串插值中的实例变量?,python,Python,我想在实例方法中使用字符串插值:但我不确定如何访问实例变量 下面是包含构造函数的基本类声明: class Validation: def __init__(self, name, type, count, script): self.name = name self.type = type self.count = count self.script = script 这里是我们想要访问变量列表的地方。注意“globals

我想在实例方法中使用字符串插值:但我不确定如何访问实例变量

下面是包含构造函数的基本类声明:

class Validation:
    def __init__(self, name, type, count, script):
        self.name = name
        self.type = type
        self.count = count
        self.script = script
这里是我们想要访问变量列表的地方。注意“globals()”不正确

    def __repr__(self):
        return "%(self.name)s: %(self.type)s that expects %(self.count)s Rows [%(self.script)s]" %self.__dict__
但这是个例外

(<type 'exceptions.KeyError'>, KeyError('self.name',), None)
(,KeyError('self.name',),无)
所以问题是:这里应该用什么来代替%self.dict

首先,已被替换为,所以让我们更新一下:

"{self.name}: {self.type} that expects {self.count} Rows [{self.script}]".format(**self.__dict__)
其次,由于您给它提供了dict来使用,dict中的键只是变量名,而不是
self.varname

"{name}: {type} that expects {count} Rows [{script}]".format(**self.__dict__)
第三,
\uuu repr\uuu
是用于复制对象的有效ish Python代码。也许你是想把它写成
\uuu str\uuu
\uuuu repr\uuuu
应该是

"Validation({name},{type},{count},{script})".format(**self.__dict__)
或者说实话,既然这是一个坏习惯,那么:

"Validation({},{},{},{})".format(self.name,self.type,self.count,self.script)
让我们试试看:

#DEMO
class Validation(object):
    def __init__(self, name, type_, count, script):
        self.name = name
        self.type = type_
        self.count = count
        self.script = script
    def __repr__(self):
        return "Validation({},{},{},{})".format(self.name,self.type,self.count,self.script)
    def __str__(self):
        return "{}: {} that expects {} Rows [{}]".format(self.name,self.type,self.count,self.script)

>>> v = Validation('MyName','integer',26,lambda x: x)
>>> print(v) # or str(v)
MyName: integer that expects 26 Rows [<function <lambda> at 0x02D75468>]
>>> repr(v)
'Validation(MyName,integer,26,<function <lambda> at 0x02D75468>)'
#演示
类验证(对象):
定义初始化(self,name,type,count,script):
self.name=名称
self.type=type_
self.count=计数
self.script=脚本
定义报告(自我):
返回“Validation({},{},{},{})”格式(self.name、self.type、self.count、self.script)
定义(自我):
返回“{}:{},需要{}行[{}].”格式(self.name、self.type、self.count、self.script)
>>>v=验证('MyName','integer',26,λx:x)
>>>打印(v)#或str(v)
MyName:需要26行[]的整数
>>>报告(五)
'验证(MyName,整数,26,)'
首先,已被替换为,所以让我们更新一下:

"{self.name}: {self.type} that expects {self.count} Rows [{self.script}]".format(**self.__dict__)
其次,由于您给它提供了dict来使用,dict中的键只是变量名,而不是
self.varname

"{name}: {type} that expects {count} Rows [{script}]".format(**self.__dict__)
第三,
\uuu repr\uuu
是用于复制对象的有效ish Python代码。也许你是想把它写成
\uuu str\uuu
\uuuu repr\uuuu
应该是

"Validation({name},{type},{count},{script})".format(**self.__dict__)
或者说实话,既然这是一个坏习惯,那么:

"Validation({},{},{},{})".format(self.name,self.type,self.count,self.script)
让我们试试看:

#DEMO
class Validation(object):
    def __init__(self, name, type_, count, script):
        self.name = name
        self.type = type_
        self.count = count
        self.script = script
    def __repr__(self):
        return "Validation({},{},{},{})".format(self.name,self.type,self.count,self.script)
    def __str__(self):
        return "{}: {} that expects {} Rows [{}]".format(self.name,self.type,self.count,self.script)

>>> v = Validation('MyName','integer',26,lambda x: x)
>>> print(v) # or str(v)
MyName: integer that expects 26 Rows [<function <lambda> at 0x02D75468>]
>>> repr(v)
'Validation(MyName,integer,26,<function <lambda> at 0x02D75468>)'
#演示
类验证(对象):
定义初始化(self,name,type,count,script):
self.name=名称
self.type=type_
self.count=计数
self.script=脚本
定义报告(自我):
返回“Validation({},{},{},{})”格式(self.name、self.type、self.count、self.script)
定义(自我):
返回“{}:{},需要{}行[{}].”格式(self.name、self.type、self.count、self.script)
>>>v=验证('MyName','integer',26,λx:x)
>>>打印(v)#或str(v)
MyName:需要26行[]的整数
>>>报告(五)
'验证(MyName,整数,26,)'

您应该省略
self.
并使用而不是
\uuuu dict\uuuu
(感谢@roippi):

印刷品:

name: type that expects count Rows [script]

您应该省略
self.
并使用而不是
\uuuu dict\uuuu
(感谢@roippi):

印刷品:

name: type that expects count Rows [script]

您可以省略
self.
“%(名称)s:%(类型)s,它需要%(计数)s行[%(脚本)s]%self.\uuu dict\uuuu
。@alecxe所说的。另外,
vars(self)
在视觉上比
self好一点。为什么不
返回“%s:%s,期望%s行[%s]”%(self.name,self.type,self.count,self.script)
?它与您已经编写的内容一样短,而且更便于重构。避免直接使用
\uu dict\uu
。如果其中一个是
属性
而不是实例属性(或者如果类使用
\uuuuuu插槽
),则变量将不在
\uuuuuu dict\uuu
中。实际上,使用
vars
也是如此。最好只显式地传递您想要使用的变量。@alecxe请给出一个答案,我会接受它(还有roippi的调整)。您可以省略
self。
“%(名称)s:%(类型)s,期望%(计数)s行[%(脚本)s]%self.\uu dict\uuu
。@alecxe说了什么。另外,
vars(self)
在视觉上比
self好一点。为什么不
返回“%s:%s,期望%s行[%s]”%(self.name,self.type,self.count,self.script)
?它与您已经编写的内容一样短,而且更便于重构。避免直接使用
\uu dict\uu
。如果其中一个是
属性
而不是实例属性(或者如果类使用
\uuuuuu插槽
),则变量将不在
\uuuuuu dict\uuu
中。实际上,使用
vars
也是如此。最好只显式地传递您想要使用的变量。@alecxe请回答这个问题,我会接受它(还有roippi的调整)