Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.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中的args值_Python_Python 2.7 - Fatal编程技术网

无法访问python中的args值

无法访问python中的args值,python,python-2.7,Python,Python 2.7,我有一个班级团队,我想为它写一个单元测试, 类名team.py class Team() def __init__(self, args): if args.player: self.player_name = args.player self.sports = '' def test__init__(self): test1 = team.Team(player="deepak") 执行此操作时,我会遇到如下错误: A

我有一个班级团队,我想为它写一个单元测试, 类名team.py

class Team()
    def __init__(self, args):
        if args.player:
            self.player_name = args.player
        self.sports = ''

def test__init__(self):
    test1 = team.Team(player="deepak")
执行此操作时,我会遇到如下错误:

AttributeError:“'str'对象没有属性'player'”


我知道这是非常基本的,但不是我需要一些快速的帮助来使用这个。我不知道在测试文件中创建对象时如何访问它。

参数和关键字参数在python中的工作方式不同。最接近您想要的是使用kwargs获取参数的dict

class Team():
    def __init__(self, **kwargs):
        if kwargs["player"]:
            self.player_name = kwargs["player"]
        self.sports = ""

test1 = Team(player="deepak")
有关python类如何工作的一些好文档,请参见

编辑

由于无法编辑代码,因此需要将args作为成员播放器的对象

from collections import namedtuple

class Team():
    def __init__(self, args):
        if args.player:
            self.player_name = args.player
        self.sports = ""

TeamArguments = namedtuple("TeamArguments", ["player"])
test1 = Team(TeamArguments(player="deepak"))


我建议从(和)开始;关键字参数不是这样工作的。
def_uuuinit_uuu(self,*args,**kwargs):如果kwargs['player']…
@tendousb
kwargs
是一个字典,那么您可以通过key not属性访问成员。
Team()
的参数应该是具有
player
属性的类的实例。它不应该是字符串。谢谢,但这是我现在需要编写的代码,我无法将任何内容更改为team.py,请建议如何使用test1=team.team(“我应该在这里提到什么”),当然,谢谢,我知道这个**kwargs,但我不知道def u init_uu(self,args),这就是我需要帮助的原因,我不能修改Team.py。我只想使用测试文件对象访问它:)@Deepak Mourya您是说
def\uuu init\uuu(self,*args)
和“
*
”吗?如果你正在传递
args.player
中的
def\uuu init\uuuuu(self,args)
那么你传递到
args中的对象需要是一个成员为
player
@Kirk-hmm的对象不,这是def\uu init\uuuuuu(self,args)
的对象:不*事实上,我现在只是想知道或者构造一下,通过这里访问这个,test1=team.team(“我在这里应该提到什么”),@Kirk yea似乎不错,如果我们需要添加更多的属性,比如player,那么我们如何才能添加更多,TeamArguments=namedtuple(“TeamArguments”),比如player=“deepak”,hobby=“tensiness”等。test=Teaam(TeamArguements)(player=“deepak”,hobby=“tenties,)//与其他args属性类似