Re: [Tutor] Why is an instance smaller than the sum of its components?

2015-02-03 Thread Jugurtha Hadjar
Sorry for the full lines. They were wrapped here but were sent unfolded. It seems I need to rewrap on Thunderbird. -- ~Jugurtha Hadjar, ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options:

Re: [Tutor] Why is an instance smaller than the sum of its components?

2015-02-03 Thread Jugurtha Hadjar
On 02/04/2015 12:18 AM, Steven D'Aprano wrote: Not necessarily. Consider: class A(object): spam = 23 def __init__(self): self.eggs = 42 In this case, the spam attribute is on the class, not the instance, and so it doesn't matter how many A instances you have, there is only

Re: [Tutor] Why is an instance smaller than the sum of its components?

2015-02-03 Thread Emile van Sebille
On 2/3/2015 1:12 PM, Jugurtha Hadjar wrote: Hello, I was writing something and thought: Since the class had some 'constants', and multiple instances would be created, I assume that each instance would have its own data. So this would mean duplication of the same constants? If so, I thought why

Re: [Tutor] Why is an instance smaller than the sum of its components?

2015-02-03 Thread Danny Yoo
Summary questions: 1 - Why are foo's and bar's class sizes the same? (foo's just a nop) i'm not sure on this one. 2 - Why are foo() and bar() the same size, even with bar()'s 4 integers? neither foo() nor bar() return anything explicitly, so both return the default none This needs

Re: [Tutor] Why is an instance smaller than the sum of its components?

2015-02-03 Thread Zachary Ware
On Tue, Feb 3, 2015 at 3:59 PM, Emile van Sebille em...@fenx.com wrote: On 2/3/2015 1:12 PM, Jugurtha Hadjar wrote: 2 - Why are foo() and bar() the same size, even with bar()'s 4 integers? neither foo() nor bar() return anything explicitly, so both return the default none This is not

Re: [Tutor] Why is an instance smaller than the sum of its components?

2015-02-03 Thread Peter Otten
Jugurtha Hadjar wrote: Hello, I was writing something and thought: Since the class had some 'constants', and multiple instances would be created, I assume that each instance would have its own data. So this would mean duplication of the same constants? If so, I thought why not put the

Re: [Tutor] Why is an instance smaller than the sum of its components?

2015-02-03 Thread Dave Angel
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,

Re: [Tutor] Why is an instance smaller than the sum of its components?

2015-02-03 Thread Jugurtha Hadjar
On 02/03/2015 11:28 PM, Zachary Ware wrote: For the OP: while this will probably be a nice exercise for learning more about Python's internals, please keep in mind that 9 times out of 10 you won't need to worry about memory usage in Python, especially not before you've proven to yourself that

Re: [Tutor] Why is an instance smaller than the sum of its components?

2015-02-03 Thread Jugurtha Hadjar
On 02/03/2015 10:57 PM, Danny Yoo wrote: But what is the documented behavior of sys.getsizeof? Reading... https://docs.python.org/3/library/sys.html#sys.getsizeof Ah ! I was reading this: https://docs.python.org/2/library/sys.html#sys.getsizeof The unlocking phrase: Only

Re: [Tutor] Why is an instance smaller than the sum of its components?

2015-02-03 Thread Steven D'Aprano
On Tue, Feb 03, 2015 at 10:12:09PM +0100, Jugurtha Hadjar wrote: Hello, I was writing something and thought: Since the class had some 'constants', and multiple instances would be created, I assume that each instance would have its own data. So this would mean duplication of the same

Re: [Tutor] Why is an instance smaller than the sum of its components?

2015-02-03 Thread Jugurtha Hadjar
On 02/03/2015 11:40 PM, Peter Otten wrote: CPython already does this for many common values, e. g. small integers and variable names a = 42 b = 42 a is b True a = 300 b = 300 a is b False The threshold seems to be 256 (last value where it evaluates to True): a = 1 b = 1 same = True

Re: [Tutor] Why is an instance smaller than the sum of its components?

2015-02-03 Thread Danny Yoo
class bar(object): ... def __init__(self): ... self.w = 5 ... self.x = 6 ... self.y = 7 ... self.z = 8 sys.getsizeof(bar()) 28 3 - Why's bar()'s size smaller than the sum of the sizes of 4 integers? But what is the documented behavior