使用sys.getsizeof(Var)方法的python大小的ctypes与`ctypes.sizeof(Var)` [英] ctypes in python size with the `sys.getsizeof(Var)` method vs `ctypes.sizeof(Var)`

查看:132
本文介绍了使用sys.getsizeof(Var)方法的python大小的ctypes与`ctypes.sizeof(Var)`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对python中的可变大小有疑问,我使用Ctypes是因为我想要一个1字节的数字,但是当我尝试在python中检查它的大小时(通过 sys.getsize )说是80个字节,但是当我检查ctypes时(通过 ctypes.sizeof ),它说只有1个字节,有人可以告诉我有什么区别,为什么有2种不同的尺码?是因为python使用了对象还是包装器?

I have a question about variable size in python, I'm using the the Ctypes because i want a 1 byte number, but when I tried to check it's size in python (via sys.getsize) it said it was 80 byte but when i checked with the ctypes (via ctypes.sizeof) it said it was 1 byte only, can someone tell me what is the difference and why is there 2 different sizes? is it because python is using an object or wrapper? and when its sent to c it views the real size?

import sys
import ctypes

print("size in ctypes is : ",ctypes.sizeof(ctypes.c_byte(1)))
print("size in sys is : ",sys.getsizeof(ctypes.c_byte(1)))

结果

size in ctypes is :  1
size in sys is :  80


推荐答案

如果您想了解详细信息,则应查看 objects.h (尤其是文件顶部的注释)。您的 ctypes.c_byte(1)是一个Python对象:

If you want to know the details, you should take a look at objects.h (especially the comments at the top of the file). Your ctypes.c_byte(1) is a Python object:

>>> import sys
>>> import ctypes
>>> isinstance(ctypes.c_byte(1), object)
True

如@Daniel所述, sys.getsizeof 获取该Python对象的大小。该Python对象大于C中相应的对象。请注意 object.h 注释中的以下内容:

As noted by @Daniel, sys.getsizeof gets the size of that Python object. That Python object is larger than the corresponding object in C. Note the following from the object.h comments:

Objects are structures allocated on the heap. . . .
The actual memory allocated for an object
contains other data that can only be accessed after casting the pointer
to a pointer to a longer structure type.  This longer type must start
with the reference count and type fields; the macro PyObject_HEAD should be
used for this.

换句话说,宏 PyObject_HEAD 是附加到每个对象的开头。另一方面,这会增加Python对象的大小。

In other words, a macro PyObject_HEAD is attached to the start of every object. This increases the size of the Python object.

ctypes.sizeof 返回实际大小Python对象内的 C 数据类型(使用C的 sizeof 运算符)。

ctypes.sizeof, on the other hand, returns the actual size of the C data type that is within the Python object (using C's sizeof operator).

编辑

根据您在Daniel的评论中提到的目标,有可能在Python 3.x中通过服务器发送一个字节。以下是使用Python的 socket 模块发送字节以证明这一点的示例。

In light of your goal that you mention in your comment to Daniel's post, it is possible to send one byte over a server in Python 3.x. Below is an example of how you would send a byte using Python's socket module to prove this point.

这里是服务器,您将在一个Python解释器中运行该服务器:

Here is the server, which you will run in one Python interpreter:

# Server
import socket

HOST = ''                      # All available interfaces
PORT = 50007                   # Same port as client
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print('Connected by', addr)
while True:
    data = conn.recv(1)        # receive data with bufsize 1; a larger bufsize will break this code
    if not data: break
    conn.sendall(data)
conn.close()

这里是客户端,您将在另一个python解释器中运行它:

Here is the client, which you will run in another python interpreter:

# Client
import socket

HOST = '127.0.0.1'             # The remote host, but here using localhost
PORT = 50007                   # The port used by both the client and server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall(b'1')                # a bytes object
data = s.recv(1)               # Receive data from the socket with bufsize of 1
s.close()
print('Received', repr(data))  # confirm receipt

这篇关于使用sys.getsizeof(Var)方法的python大小的ctypes与`ctypes.sizeof(Var)`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆