Hi, -
class myclass:
-
def __init__(self,i,j):
-
ival = i
-
sstrng = j
-
-
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.
4 3840
Hi, -
class myclass:
-
def __init__(self,i,j):
-
ival = i
-
sstrng = j
-
-
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.
I've never programmed C but I think you want the len function. -
>>> len([1,2,3])
-
3
-
>>> len("A string")
-
8
-
>>> len((1,2,3))
-
3
-
>>> len({'a':1,'b':2,'c':3})
-
3
-
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 -
class myclass:
-
def __init__(self,i,j):
-
self.ival = i
-
self.sstrng = j
-
-
I've never programmed C but I think you want the len function. -
>>> len([1,2,3])
-
3
-
>>> len("A string")
-
8
-
>>> len((1,2,3))
-
3
-
>>> len({'a':1,'b':2,'c':3})
-
3
-
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 -
class myclass:
-
def __init__(self,i,j):
-
self.ival = i
-
self.sstrng = j
-
-
Thanx for your reply
but still i didn't get things -
import string
-
class test2:
-
ival = None
-
sstr = None
-
def __init__(self,i,j):
-
self.ival = i
-
self.sstr = j
-
def start():
-
-
obj = test2(10,"the test2")
-
print len(obj)
-
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__'
Thanx for your reply
but still i didn't get things -
import string
-
class test2:
-
ival = None
-
sstr = None
-
def __init__(self,i,j):
-
self.ival = i
-
self.sstr = j
-
def start():
-
-
obj = test2(10,"the test2")
-
print len(obj)
-
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: -
class test2:
-
def __init__(self, value, name):
-
self.ival = value
-
self.sstr = name
-
-
def __len__(self):
-
nBytes = self.ival/256 + 1
-
return len(self.sstr) + nBytes
-
-
-
obj = test2(255, "the test2")
-
print len(obj)
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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:...
|
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.
|
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....
|
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...
|
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...
|
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...
|
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...
|
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...
|
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"....
|
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...
|
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 =...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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...
| |