[Tutor] Why is an instance smaller than the sum of its components?
Dave Angel
davea at davea.name
Wed Feb 4 00:21:13 CET 2015
On 02/03/2015 04:12 PM, Jugurtha Hadjar wrote:
> Hello,
>
Lots of other good comments, so I'll just remark on one point.
>
> >>> class bar(object):
> ... def __init__(self):
> ... self.w = 5
> ... self.x = 6
> ... self.y = 7
> ... self.z = 8
>
If these really are "constants," meaning the same value for all
instances, then you can make them class attributes instead of instance
attributes. I'd advise doing this to avoid bugs, not to save memory,
though if you have lots of instances, it'll certainly save memory.
>>> class bar(object):
... w = 5
... x = 6
... def __init__(self):
... self.y = 7
... self.z = 8
At this point, all instances will have a self.w of 5, and a self.x of 6.
But they'll each have a self.y and self.z which could change
independently.
--
DaveA
More information about the Tutor
mailing list