标签: python-internals

__dir__ 到底是如何实现的,我应该如何知道它?

所以我想了解dir()功能的细节。首先我查看了它的实现: https://github.com/python/cpython/blob/e76daebc0c8afa3981a4c5a8b54537f756e805de/Objects/object.c#L1450-L1477

/* Helper for PyObject_Dir: object introspection. */
static PyObject *
_dir_object(PyObject *obj)
{
    PyObject *result, *sorted;
    PyObject *dirfunc = _PyObject_LookupSpecial(obj, &PyId___dir__);

    assert(obj);
    if (dirfunc == NULL) {
        if (!PyErr_Occurred())
            PyErr_SetString(PyExc_TypeError, "object does not provide __dir__");
        return NULL;
    }
    /* use __dir__ */
    result = _PyObject_CallNoArg(dirfunc);
    Py_DECREF(dirfunc);
    if (result == NULL)
        return NULL;
    /* return sorted(result) */
    sorted = PySequence_List(result);
    Py_DECREF(result);
    if (sorted == NULL)
        return NULL;
    if (PyList_Sort(sorted)) {
        Py_DECREF(sorted);
        return NULL;
    }
    return sorted;
} …
Run Code Online (Sandbox Code Playgroud)

python python-internals

2
推荐指数
1
解决办法
2126
查看次数

Python - 为什么不总是缓存所有不可变对象?

我不确定下面代码的 Python 对象模型在幕后发生了什么。

您可以从此链接下载 ctabus.csv 文件的数据

import csv

def read_as_dicts(filename):
    records = []
    with open(filename) as f:
        rows = csv.reader(f)
        headers = next(rows)

        for row in rows:
            route = row[0]
            date = row[1]
            daytype = row[2]
            rides = int(row[3])
            records.append({
                    'route': route,
                    'date': date,
                    'daytype': daytype,
                    'rides': rides})

    return records

# read data from csv
rows = read_as_dicts('ctabus.csv')
print(len(rows)) #736461

# record route ids (object ids)
route_ids = set()
for row in rows:
    route_ids.add(id(row['route']))

print(len(route_ids)) #690072

# unique_routes …
Run Code Online (Sandbox Code Playgroud)

python string caching python-internals

2
推荐指数
1
解决办法
325
查看次数

在哪里可以找到 os.urandom() 的源代码?

我想调查研究python 3.7的os.urandom()函数代码。我查看了各自的标准库的os.py,但它既没有在那里定义,也没有在那里导入。我也尝试 grep 定义:

/usr/lib/python3.7 $ grep -rFl "def urandom" 2> /dev/null 但没什么。我怀疑 os.urandom() 发生了一些神奇的事情。在哪里可以找到用于学习和密码分析的函数定义?

python random python-3.x python-internals

2
推荐指数
1
解决办法
869
查看次数

如何在 Python 包中创建和加载内部数据

我正在开发一个 Python 包,Python 包的实现取决于一些研究结果。我将研究结果保存在 Python 字典中。我有两个问题:

  1. 如何将此字典保存为我的包中的内部数据?
  2. 在包中编写函数时,如何加载这些内部数据?

我研究了这个,但仍然不知道如何从头开始保存包内部数据。它也没有显示如何加载保存的内部数据。devtools::use_data在 R 中有类似的东西吗?

python module package python-3.x python-internals

2
推荐指数
1
解决办法
1735
查看次数

如何在Python中有效地检查字符串是否为十六进制

我需要检查字符串是否是十六进制。我学到了两种方法 -

1.) 循环每个字符

all(c in string.hexdigits for c in s) # Straight forward with no optimizations
Run Code Online (Sandbox Code Playgroud)

2.) 使用int ()函数检查是否有错误

try:
    int(s, 16)
    return True
except ValueError:
    return False
Run Code Online (Sandbox Code Playgroud)

在第一种情况下,我知道复杂度是 O(n)。但是第二个呢?那里的时间复杂度是多少?

python python-internals

2
推荐指数
1
解决办法
4539
查看次数

什么是 _PyEval_EvalFrameDefault?

当我使用 perf 进行分析时,_PyEval_EvalFrameDefaultCPU 使用率位于顶部。

但我不知道那是什么。我怎样才能避免这个功能?

python profiling python-3.x python-internals perf

2
推荐指数
1
解决办法
5065
查看次数

无法深度复制同时定义了 __init__ 和 __new__ 的类

我遇到了(在我看来)一个有点奇怪的问题。我定义了一个同时定义了initnew 的类,如下所示:

class Test:

    def __init__(self, num1):
        self.num1 = num1

    def __new__(cls, *args, **kwargs):
        new_inst = object.__new__(cls)
        new_inst.__init__(*args, **kwargs)
        new_inst.extra = 2
        return new_inst
Run Code Online (Sandbox Code Playgroud)

如果正常使用,效果很好:

test = Test(1)
assert test.extra == 2
Run Code Online (Sandbox Code Playgroud)

但是,它不会复制。deepcopy:

import copy
copy.deepcopy(test)
Run Code Online (Sandbox Code Playgroud)

给出

TypeError: __init__() missing 1 required positional argument: 'num1'
Run Code Online (Sandbox Code Playgroud)

这可能与使用类包装器和 __new__ 装饰类有关- 我无法确切地看到如何,但我在这里尝试类似的事情 - 我需要new将类包装器应用到我创建的测试实例。

任何帮助感激不尽!

python copy new-operator python-internals

2
推荐指数
1
解决办法
1961
查看次数

为什么字符串乘法的字节码不同?

我正在摆弄字符串并遇到以下行为:

>>> import dis
>>> dis.dis('"abcdefgh" * 513')
  1           0 LOAD_CONST               0 ('abcdefgh')
              2 LOAD_CONST               1 (513)
              4 BINARY_MULTIPLY
              6 RETURN_VALUE
>>> dis.dis('"abcdefgh" * 512')
  1           0 LOAD_CONST               0 ('abcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh')
              2 RETURN_VALUE
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,当我乘以 时"abcdefgh"512python 在编译阶段预先计算完整的字符串,但是当我将数字更改为513它时,它不再这样做,而是生成两个LOAD_CONSTBINARY_MULTIPLY操作码(换句话说,python 不再预先计算编译阶段的结果)。有人可以解释这种行为的原因吗?

注意:我知道从代码的角度来看它工作得很好,但我很想知道这种行为的原因。

python cpython compiler-optimization python-3.x python-internals

2
推荐指数
1
解决办法
141
查看次数

带有空间的类属性

是否可以在 Python 中执行类似以下操作?

class Object:
    
    def `two words`(self):
        return 'Worked!'
Run Code Online (Sandbox Code Playgroud)

根据方言,在 SQL 中,您通常可以使用类似Person.[two words]Person.`two words`等的东西来做到这一点。在 python 中可以做到这一点吗?

python python-3.x python-internals

2
推荐指数
1
解决办法
744
查看次数

pip 如何告诉 Python 如何导入 C 扩展

sysv_ipc我希望以便携的方式使用该库。

我安装了它:

pip3 install sysv_ipc

然后从Python:

import sysv_ipc
sysv_ipc.__file__

# Output:
# /home/x/.local/lib/python3.9/site-packages/sysv_ipc.cpython-39-x86_64-linux-gnu.so
Run Code Online (Sandbox Code Playgroud)

如果我将该文件复制到文件夹(pip uninstall库),然后从该文件夹打开 python 并尝试相同的导入,则会失败。

我尝试检查还安装了哪些内容,结果发现:

/home/x/.local/lib/python3.9/site-packages/sysv_ipc-1.1.0.dist-info
/home/x/.local/lib/python3.9/site-packages/sysv_ipc.cpython-39-x86_64-linux-gnu.so
/home/x/.local/lib/python3.9/site-packages/sysv_ipc-1.1.0.dist-info/INSTALLER
/home/x/.local/lib/python3.9/site-packages/sysv_ipc-1.1.0.dist-info/LICENSE
/home/x/.local/lib/python3.9/site-packages/sysv_ipc-1.1.0.dist-info/METADATA
/home/x/.local/lib/python3.9/site-packages/sysv_ipc-1.1.0.dist-info/RECORD
/home/x/.local/lib/python3.9/site-packages/sysv_ipc-1.1.0.dist-info/REQUESTED
/home/x/.local/lib/python3.9/site-packages/sysv_ipc-1.1.0.dist-info/WHEEL
/home/x/.local/lib/python3.9/site-packages/sysv_ipc-1.1.0.dist-info/top_level.txt
Run Code Online (Sandbox Code Playgroud)

我也没有在setup.py中找到线索。

我想弄清楚的是——

sysv_ipcpip 如何/在哪里与要从该特定文件导入的Python 相关?

pip python-c-api python-import python-3.x python-internals

2
推荐指数
1
解决办法
885
查看次数