English 中文(简体)
取消在沙里夫的特殊职能
原标题:Overwriting special functions in Python

是否有办法超越在座标的类型中建造的特殊功能? 例如,我要创立一个特殊分班,继承dict中建筑的遗产。 我想允许用户在我的特别字典中确定关键和价值观的习俗验证功能,例如:

def __init__(self, keyValidator = True, valueValidator = True):
    self.keyValidator = keyValidator
    self.valueValidator = valueValidator

通过这样做,我可以拦截在更新方法中增加价值的做法,例如:

def update(self,key,value):
    assert (self.keyValidator(key))
    assert (self.valueValidator(key))
    self[key] = value

但是,如果某个人决定公正走下去,利用[]进入,这就赢得了工作。 或者,有人使用字面字字面来制造物体。

mySpecialDict = SpecialDict
mySpecialDict[ hello ] = 54
最佳回答

有些人(can t)以字面字字面表示,因为字面字面上是字面;字面是普通字典。

my specialDict[ hello] = 54所示,就定有方括号的项目而言,这只是my specialDict.__setitem__( hello , 54)。 同样,将一项目与平板标记重新计算,相当于援引<代码>__getitem__方法。 无论哪类“密码”是何种类别? 因此,你只能执行这些方法,以改变其做些什么(使用<条码>(特殊分录、自订)__ 环形项目__(关键、价值)或<条码>,在需要时提及正常执行。

你们将遇到的一个问题是,在实施其他法规方法时所建立的某些(所有?)将不会尊重您的压倒性<代码>_setitem_或__get>。 因此,你获得了继承财产的机会;你不得不凌驾于所有这些财产之上,要么完全按照你的基本行动版本加以执行,要么至少进行你“四舍五入”超级阶级电话的验证。

一种不太令人痛心的做法实际上可能是而不是<>em>减去 built,而是使用<密码>收集的习惯“类似”物体。 地图或<代码>收集s.MutableMapping/code>基类,以便进入字典接口。 采用这一方法,你只需要执行大约6种基本方法,即(你通过在你向包装字典电话的验证逻辑上进行总结),并根据这些方法对其他方法作出明智的定义。 See

问题回答

页: 1

移动装置中的Im 能够提供完整的答案,但可以浏览__getitem____setitem__





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...