python – 在init中重新分配self
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 在init中重新分配self,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3339字,纯文字阅读大概需要5分钟。
内容图文
![python – 在init中重新分配self](/upload/InfoBanner/zyjiaocheng/809/d0aaffdf21df4158891966177978e550.jpg)
我有一个类在类体中有查找字典,我存储所有实例和一些键.
当我实例化实例时,我没有在任何变量或外部字典中保存它们,我将它们存储在这个查找字典中.
当我以某种方式实例化一个已经在dict中的实例时,我将它重新分配给dict中的那个并更新它,在其他函数中使用它的新旧值.
我想知道这是一个好习惯吗?或者我应该重构并让它有一个外部字典来保存实例.
这种行为有没有PEP指南?
例:
class MyClass:
all_instances = {} # this dictionary is in local space and it's clear that
# the only interaction is with MyClass
def __init__(self, unique_id, x, y, z):
if unique_id not in MyClass.all_instances:
self.unique_id = unique_id
self.x = x
self.y = y
self.z = z
MyClass.all_instances[unique_id] = self
else:
self = MyClass.all_instances[unique_id]
self.update(x, y, z)
def update(self, new_x, new_y, new_z):
self.x = self.do_something_with(self.x, new_x)
self.y = self.do_something_with(self.y, new_y)
self.z = self.do_something_with(self.z, new_z)
@staticmethod
def do_something_with(old_value, new_value):
# do something with old value and new and return some value
value = (old_value + new_value) / 2 # more complicated than tht
return value
while True:
for id in get_ids(): # fetch ids from some database
x, y, z = get_values(id) # fetch values from some other database
MyClass(id, x, y, z)
我得到id和值的数据库每次都在变化,所以我无法确定我是否会得到我已经实例化的数据,或者值不同.
我看到的方式是,类的所有功能都在其自身内部发生,不需要使用字典,因此不清楚它们与代码的任何其他部分的交互位置.
这是我通常会做的,没有重新分配它:
class MyClass:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def update(self, new_x, new_y, new_z):
self.x = self.do_something_with(self.x, new_x)
self.y = self.do_something_with(self.y, new_y)
self.z = self.do_something_with(self.z, new_z)
@staticmethod
def do_something_with(old_value, new_value):
# do something with old value and new and return some value
value = (old_value + new_value) / 2 # more complicated than tht
return value
all_instances = {} # this dictionary is now in global space
# and it's unclear if the only interaction is with MyClass
while True:
for id in get_ids(): # fetch ids from some database
x, y, z = get_values(id) # fetch values from some other database
if id not in all_instances:
all_instances[id] = MyClass(x, y, z)
else:
all_instances[id].update(x, y, z)
解决方法:
你在这里得到的是一个类似单身的模式. (我说像单身一样,因为你实际上并没有一个实例,而是一组实例键入了几个参数).有lots of reason why singletons (and global data in general) are a bad idea,所以我投票给外部注册表.
但是,如果稍微考虑一下,如果您确定要使用“内部”注册表,则可以使用__new__执行此操作.
class MyClass(object):
all_instances = {}
def __new__(cls, unique_id, x, y, z):
instance = cls.all_instances.get(unique_id)
if instance is not None:
instance.is_initialized = True
else:
instance = super(MyClass, cls).__new__(cls, unique_id, x, y, z)
instance.is_initialized = False
return instance
def __init__(self, unique_id, x, y, z):
if self.is_initialized:
return
# Do initialization here.
注意如何使用__new__来改变对象创建的发生方式.如果我们已经有了一个注册对象,我们返回它而不是创建一个新对象 – 否则,我们在超类(对象)上调用__new__,它为我们提供了一个新的(未初始化的)MyClass实例.在调用__new__之后(并假设它返回MyClass的一个实例),将调用__init__.这可能导致__init__被多次调用同一个对象(这就是所有is_initialized业务的意义所在.
is_initialized位是可选的 – 我假设您不想重置x,y,z等.但是如果这样做,您可以将其关闭.
内容总结
以上是互联网集市为您收集整理的python – 在init中重新分配self全部内容,希望文章能够帮你解决python – 在init中重新分配self所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。