Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.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/4/oop/2.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 如何为类编写通用get方法?_Python_Oop_Methods_Args - Fatal编程技术网

Python 如何为类编写通用get方法?

Python 如何为类编写通用get方法?,python,oop,methods,args,Python,Oop,Methods,Args,最近我开始在python中使用oop。我想为启动的属性编写一个通用的get或set方法。例如,有一个具有多个属性和相应get方法的类Song。我希望避免使用多个if语句。只有两个属性并不重要,但是如果属性超过5个,代码将很难阅读。是否可以使用args中的字符串从init获取值,而不定义所有可能的情况 class Song(object): def __init__(self,title,prod): self.title = title self.pro

最近我开始在python中使用oop。我想为启动的属性编写一个通用的get或set方法。例如,有一个具有多个属性和相应get方法的类Song。我希望避免使用多个if语句。只有两个属性并不重要,但是如果属性超过5个,代码将很难阅读。是否可以使用args中的字符串从init获取值,而不定义所有可能的情况

class Song(object):

    def __init__(self,title,prod):
        self.title = title
        self.prod = prod

    def getParam(self,*args):
        retPar = dict()
        if 'title' in args: 
            print(self.title)
            retPar['title'] = self.title
        if 'prod' in args:
            print(self.prod)
            retPar['prod'] = self.prod
        return(retPar)
我不确定这是否可能,因为我什么也找不到。我该怎么做

为不熟悉python语法的同事提供一个函数,这样他们就不必直接访问属性。为了策划之类的事情

因为python不一定是教授的,而且点语法对于掌握matlab基本知识的人来说是令人困惑的

我认为这是很容易教的,你不应该为了这么简单的事情而弯腰…但假设这真的是你想要的,这看起来是一个更好的主意:

class Song(object):
    def __init__(self, title, prod):
        self.title = title
        self.prod = prod
    
    def __getitem__(self, key):
        return getattr(self, key)

song = Song('Foo', 'Bar')
print song['title']
见和

为不熟悉python语法的同事提供一个函数,这样他们就不必直接访问属性。为了策划之类的事情

因为python不一定是教授的,而且点语法对于掌握matlab基本知识的人来说是令人困惑的

我认为这是很容易教的,你不应该为了这么简单的事情而弯腰…但假设这真的是你想要的,这看起来是一个更好的主意:

class Song(object):
    def __init__(self, title, prod):
        self.title = title
        self.prod = prod
    
    def __getitem__(self, key):
        return getattr(self, key)

song = Song('Foo', 'Bar')
print song['title']

请参阅和。

我不确定您为什么要这样做。为什么需要一个方法?
返回{attr:getattr(self,attr)for attr in args}
。@Daniel Roseman:为不熟悉python语法的同事提供一个函数,这样他们就不必直接访问attritubes。“我想为启动的属性编写一个通用的get或set方法”——为什么?这不是Java,要么直接访问它们,要么使用
@property
。为什么连最基本的Python语法都不熟悉的人都会编写Python呢?attrs=song.getParam('title','prod');attrs['title']是否比
歌曲更好或更简单。title
?我不确定您为什么要这样做。为什么需要一个方法?
返回{attr:getattr(self,attr)for attr in args}
。@Daniel Roseman:为不熟悉python语法的同事提供一个函数,这样他们就不必直接访问attritubes。“我想为启动的属性编写一个通用的get或set方法”——为什么?这不是Java,要么直接访问它们,要么使用
@property
。为什么连最基本的Python语法都不熟悉的人都会编写Python呢?attrs=song.getParam('title','prod');attrs['title']比歌曲更好或更容易吗?