471,349 Members | 1,236 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,349 software developers and data experts.

how to get the size of an object

Hi,

Expand|Select|Wrap|Line Numbers
  1. class myclass:
  2.      def __init__(self,i,j):
  3.           ival = i
  4.           sstrng = j
  5.  
  6.  
this is a very simple class
if i create object like
obj = myclass(15,"for test")

is there any operator like sizeof() in c, which tells about the size of the object
if not how can i get the size of object.
Oct 15 '07 #1
4 3753
bartonc
6,596 Expert 4TB
Hi,

Expand|Select|Wrap|Line Numbers
  1. class myclass:
  2.      def __init__(self,i,j):
  3.           ival = i
  4.           sstrng = j
  5.  
  6.  
this is a very simple class
if i create object like
obj = myclass(15,"for test")

is there any operator like sizeof() in c, which tells about the size of the object
if not how can i get the size of object.
If you need C-like operations, you can use the ctypes module. We use it for the ability to call C libraries with python data structures.
Oct 15 '07 #2
elcron
43
I've never programmed C but I think you want the len function.
Expand|Select|Wrap|Line Numbers
  1. >>> len([1,2,3])
  2. 3
  3. >>> len("A string")
  4. 8
  5. >>> len((1,2,3))
  6. 3
  7. >>> len({'a':1,'b':2,'c':3})
  8. 3
  9.  
len calls on the __len__ function of an object for operator overloading. Don't forget to add self.* to save a value to the class instance. I believe this is closer to what you want
Expand|Select|Wrap|Line Numbers
  1. class myclass:
  2.      def __init__(self,i,j):
  3.           self.ival = i
  4.           self.sstrng = j
  5.  
  6.  
Oct 15 '07 #3
I've never programmed C but I think you want the len function.
Expand|Select|Wrap|Line Numbers
  1. >>> len([1,2,3])
  2. 3
  3. >>> len("A string")
  4. 8
  5. >>> len((1,2,3))
  6. 3
  7. >>> len({'a':1,'b':2,'c':3})
  8. 3
  9.  
len calls on the __len__ function of an object for operator overloading. Don't forget to add self.* to save a value to the class instance. I believe this is closer to what you want
Expand|Select|Wrap|Line Numbers
  1. class myclass:
  2.      def __init__(self,i,j):
  3.           self.ival = i
  4.           self.sstrng = j
  5.  
  6.  

Thanx for your reply
but still i didn't get things
Expand|Select|Wrap|Line Numbers
  1. import string
  2. class test2:
  3.     ival = None
  4.     sstr = None
  5.     def __init__(self,i,j):
  6.         self.ival = i
  7.         self.sstr = j
  8. def start():
  9.  
  10.     obj = test2(10,"the test2")
  11.     print len(obj)
  12.  
it says
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "test2.py", line 11, in start
print len(obj)
AttributeError: test2 instance has no attribute '__len__'
Oct 16 '07 #4
bartonc
6,596 Expert 4TB
Thanx for your reply
but still i didn't get things
Expand|Select|Wrap|Line Numbers
  1. import string
  2. class test2:
  3.     ival = None
  4.     sstr = None
  5.     def __init__(self,i,j):
  6.         self.ival = i
  7.         self.sstr = j
  8. def start():
  9.  
  10.     obj = test2(10,"the test2")
  11.     print len(obj)
  12.  
it says
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "test2.py", line 11, in start
print len(obj)
AttributeError: test2 instance has no attribute '__len__'
This won't work to interface to a C library, but it's a close approximation:
Expand|Select|Wrap|Line Numbers
  1. class test2:
  2.     def __init__(self, value, name):
  3.         self.ival = value
  4.         self.sstr = name
  5.  
  6.     def __len__(self):
  7.         nBytes = self.ival/256 + 1
  8.         return len(self.sstr) + nBytes
  9.  
  10.  
  11. obj = test2(255, "the test2")
  12. print len(obj)
Oct 16 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

18 posts views Thread by Tarundeep | last post: by
2 posts views Thread by Kums | last post: by
6 posts views Thread by Jon Jagger | last post: by
6 posts views Thread by cameron | last post: by
4 posts views Thread by tshad | last post: by
8 posts views Thread by Dave | last post: by
6 posts views Thread by Scirious | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.