python - Reassign self in init -
i have class has lookup dict in class body, store instances , key.
when instantiate instances don't hold them in variable or external dict, store them in lookup dict.
when somehow instantiate instance in dict, reassign 1 in dict , update it, using it's new , old value in other function.
i wonder if practice? or should refactor , make have external dict hold instances.
is there pep guide kind of behavior?
example:
class myclass: all_instances = {} # dictionary in local space , it's clear # interaction 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): # old value , new , return value value = (old_value + new_value) / 2 # more complicated tht return value while true: id in get_ids(): # fetch ids database x, y, z = get_values(id) # fetch values other database myclass(id, x, y, z)
the databases ids , values changing every time, can't know sure if i'll ones have instantiate, or values different.
the way see, functionality of class happens within itself, no need have dictionaries laying around, making unclear interacting other part of code.
this how do, without reassigning it:
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): # old value , new , return value value = (old_value + new_value) / 2 # more complicated tht return value all_instances = {} # dictionary in global space # , it's unclear if interaction myclass while true: id in get_ids(): # fetch ids database x, y, z = get_values(id) # fetch values other database if id not in all_instances: all_instances[id] = myclass(x, y, z) else: all_instances[id].update(x, y, z)
what you've got here singleton-like pattern. (i singleton-like because don't have 1 instance, set of instances keyed off couple of parameters). there lots of reason why singletons (and global data in general) bad idea, i'd vote external registry.
however, if after thinking bit, if decide do want use "internal" registry, can using __new__
.
class myclass(object): all_instances = {} def __new__(cls, unique_id, x, y, z): instance = cls.all_instances.get(unique_id) if instance 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 # initialization here.
notice how __new__
used change how object creation happens. if have registered object, return rather creating new object -- otherwise, call __new__
on super class (object
) gives new (uninitialized) instance of myclass
. after __new__
has been called (and assuming returns instance of myclass
), __init__
called. can lead __init__
being called multiple times same object (that's is_initialized
business about.
the is_initialized
bit optional -- i'm assuming don't want reset x
, y
, z
, etc. if do, can leave off.
Comments
Post a Comment