473,289 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 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 3840
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

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

Similar topics

35
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I...
18
by: Tarundeep | last post by:
hi, let us say it is a 32 bit processor then the size of the pointer is 32 bits. now i want to know what would be the size of the class with vtable pointer in it , that is it has few virtual...
2
by: hvaisane | last post by:
Valgrind says ==11604== Invalid read of size 4 ==11604== at 0x8048ABB: main (foo.cc:36) ==11604== Address 0x1B92415C is 4 bytes inside a block of size 8 free'd ==11604== at 0x1B90514F:...
2
by: Kums | last post by:
What is the maximum permissible size of a database? Is there any limitation. What is the maximum # of tablespace's allowed in a database? Thanks for your response.
6
by: Jon Jagger | last post by:
I was thinking about how you can only use sizeof to find the size of an unmanaged type. I started to wonder if there was a way to find the size of a managed object and came up with the following....
6
by: cameron | last post by:
I need to get the size of an objet in memory. I have tried: System.IO.MemoryStream m = new System.IO.MemoryStream(); System.Runtime.Serialization.Formatters.Binary.BinaryFormatter b = new...
4
by: tshad | last post by:
I am having trouble with links in my DataGrid. I have Links all over my page set to smaller and they are consistant all over the page in both Mozilla and IE, except for the DataGrid. Here is a...
5
by: Phil Jones | last post by:
Is there a way to determine the size (number of bytes) of an object? I figure this can be done by serializing the object to disk and measuring the file size - but I definately don't want to do...
8
by: Dave | last post by:
I am serialising an object to a memory mapped file (using the CreateFileMapping and MapViewOfFile p/invoke calls). These need to know the maximum size of the "file". I can put in a "good guess" ie...
6
by: Scirious | last post by:
People, how can I know how big an object is? I mean, I have an object the collects data from a stream and when it grows to an especific size I need to create a new object to continue collecting the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.