473,381 Members | 1,450 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,381 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 3845
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.