如何使用动态名称实现property()(在python中) [英] How to implement property() with dynamic name (in python)

查看:86
本文介绍了如何使用动态名称实现property()(在python中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为单个神经元编写一个仿真程序.因此,我必须处理很多参数.现在的想法是,我有两个类,一个用于SingleParameter,另一个用于参数Collection.我使用property()来轻松访问参数值并使代码更具可读性.这对于单个参数非常合适,但是我不知道如何为集合实现它,因为我想在SingleParameter之后命名Collection中的属性.这里是一个例子:

I am programming a simulations for single neurons. Therefore I have to handle a lot of Parameters. Now the Idea is that I have two classes, one for a SingleParameter and a Collection of parameters. I use property() to access the parameter value easy and to make the code more readable. This works perfect for a sinlge parameter but I don't know how to implement it for the collection as I want to name the property in Collection after the SingleParameter. Here an example:

class SingleParameter(object):
  def __init__(self, name, default_value=0, unit='not specified'):
    self.name = name
    self.default_value = default_value
    self.unit = unit
    self.set(default_value)
  def get(self):
    return self._v
  def set(self, value):
    self._v = value
  v = property(fget=get, fset=set, doc='value of parameter')

par1 = SingleParameter(name='par1', default_value=10, unit='mV')
par2 = SingleParameter(name='par2', default_value=20, unit='mA')

# par1 and par2 I can access perfectly via 'p1.v = ...'
# or get its value with 'p1.v'

class Collection(object):
  def __init__(self):
    self.dict = {}
  def __getitem__(self, name):
    return self.dict[name] # get the whole object
    # to get the value instead:
    # return self.dict[name].v
  def add(self, parameter):
    self.dict[parameter.name] = parameter
    # now comes the part that I don't know how to implement with property():
    # It shoule be something like
    # self.__dict__[parameter.name] = property(...) ?

col = Collection()
col.add(par1)
col.add(par2)
col['par1'] # gives the whole object

# Now here is what I would like to get:
# col.par1 -> should result like col['par1'].v
# col.par1 = 5 -> should result like col['par1'].v = 5


我要理解property()的其他问题:


Other questions that I put to understand property():

  • 为什么托管属性仅适用于类属性而不是python中的实例属性?
  • Why do managed attributes just work for class attributes and not for instance attributes in python?
  • How can I assign a new class attribute via __dict__ in python?

推荐答案

为两个类使用相同的get/set函数会迫使您陷入带有参数列表的丑陋黑客.非常粗略,这就是我的做法:

Using the same get/set functions for both classes forces you into an ugly hack with the argument list. Very sketchy, this is how I would do it:

在SingleParameter类中,照常定义get和set:

In class SingleParameter, define get and set as usual:

def get(self):
  return self._s
def set(self, value):
  self._s = value

在Collection类中,直到创建该属性,您才知道这些信息,因此您定义了元集/元数据函数,并仅在以后使用lambda函数对其进行特殊化:

In class Collection, you cannot know the information until you create the property, so you define the metaset/metaget function and particularize them only later with a lambda function:

def metaget(self, par):
  return par.s
def metaset(self, value, par):
  par.s = value
def add(self, par):
  self[par.name] = par
  setattr(Collection, par.name,
    property(
      fget=lambda x : Collection.metaget(x, par),
      fset=lambda x, y : Collection.metaset(x,y, par))

这篇关于如何使用动态名称实现property()(在python中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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