根据文档:
\n\n\n\n\n\n\n
inspect.currentframe()返回调用者\xe2\x80\x99s 堆栈\n 帧的帧对象。
\n\nCPython 实现细节:此函数依赖于解释器中的 Python 堆栈框架支持,但不能保证在所有 Python 实现中都存在该支持。如果在没有 Python 堆栈框架支持的实现中运行,则此函数返回 None。
\n
怎么只有这个函数被标记为“依赖于实现”?如果这个功能不行的话,类似的功能,比如inspect.trace、inspect.stack等不是也无法使用吗?
另外,“堆栈框架支持”是什么意思,为什么它会缺失?
\n%python -m timeit -s "import copy" "x = (1, 2, 3)" "copy.deepcopy(x)"
100000 loops, best of 3: 10.1 usec per loop
%python -m timeit -s "import copy" "x = (1, 2, 3)" "copy.copy(x)"
1000000 loops, best of 3: 0.609 usec per loop
Run Code Online (Sandbox Code Playgroud)
为什么deepcopy慢15倍copy?
每个函数都必须遍历元组的元素.在该迭代期间,copy创建对每个元素的另一个引用; deepcopy深刻的每个元素.
但每个元素都是一个int,而深度复制int只是创建了另一个引用.换句话说,这两个函数似乎执行完全相同的步骤,相同的次数.
这是验证过程中没有创建新实例:
ActivePython 3.2.1.2 (ActiveState Software Inc.) based on
Python 3.2.1 (default, Jul 18 2011, 14:31:09) [MSC v.1500 64 bit (AMD64)] on win32
Type …Run Code Online (Sandbox Code Playgroud) 如果您使用多处理并且意外地最终创建无限制的进程,操作系统将不会喜欢它.
是否有任何简单的解决方案可以防止这种情况发生(例如,通过限制Python或OS中的进程总数)?
我使用Windows,当我犯这样的错误时,它的表现非常糟糕(需要硬重启).所以我喜欢它,如果有一些代码可以包含/添加到我的应用程序并防止这种情况发生.
D3文档说:
在追加或插入时,输入选择会合并到更新选择中.这种方法减少了输入和更新之间的代码重复.您可以在输入节点后将其应用于更新选择,而不是将运算符分别应用于输入和更新选择.在极少数情况下,您只想在更新节点上运行运算符,您可以在更新选择之前运行它们,然后再输入新节点.
我不明白merge into第一句中短语的含义.有人可以解释这一点吗?(也许我错过了与某些标准数据库术语的连接?)
我知道在通用编程中,算法与容器分离.因此,将通用算法实现为实例方法是没有意义的(相同的算法应该适用于多个具体类;我们不希望它们都从一个ABC继承,因为它会以指数方式增加类的数量).
但是source()对于Boost图库中的函数,我不明白它为什么是全局函数而不是图类的实例方法.
据我所知,通过阅读BGL源代码,source(e, g)需要知道图形的实现细节和传递给它的边缘对象; 只知道他们的界面是不够的.
所以source()不是通用算法.换句话说,它需要知道图形实例的具体类.那么为什么不把它作为实例方法放在同一个类中呢?它不是比制作一个需要为每个被调用的类定制的全局函数更清晰/更少混淆吗?
UPDATE
相关源代码:
// dwa 09/25/00 - needed to be more explicit so reverse_graph would work.
template <class Directed, class Vertex,
class OutEdgeListS,
class VertexListS,
class DirectedS,
class VertexProperty,
class EdgeProperty,
class GraphProperty, class EdgeListS>
inline Vertex
source(const detail::edge_base<Directed,Vertex>& e,
const adjacency_list<OutEdgeListS, VertexListS, DirectedS,
VertexProperty, EdgeProperty, GraphProperty, EdgeListS>&)
{
return e.m_source;
}
namespace boost {
namespace detail {
template <typename Directed, …Run Code Online (Sandbox Code Playgroud) sklearn分类器将Pandas TimeStamp(= datetime64[ns])接受为X中的列,只要所有 X列都属于该类型。但是,当同时有TimeStamp和float列时,sklearn拒绝使用TimeStamp。
除了将时间戳转换为int使用astype(int)之外,还有其他解决方法吗?(我仍然需要原始列来访问dt.year等,因此理想情况下,最好不要创建重复列只是为了向sklearn提供功能。)
import pandas as pd
from sklearn.linear_model import LinearRegression
test = pd.date_range('20000101', periods = 100)
test_df = pd.DataFrame({'date': test})
test_df['a'] = 1
test_df['y'] = 1
lr = LinearRegression()
lr.fit(test_df[['date']], test_df['y']) # works fine
lr.fit(test_df[['date', 'date']], test_df['y']) # works fine
lr.fit(test_df[['date', 'a']], test_df['y']) # complains
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-90-0605fa5bcdfa> in <module>()
----> 1 lr.fit(test_df[['date', 'a']], test_df['y'])
/home/shoya/.pyenv/versions/3.5.0/envs/study-env/lib/python3.5/site-packages/sklearn/linear_model/base.py in fit(self, X, …Run Code Online (Sandbox Code Playgroud) 我想指定类型提示的功能,可以接收无论是list的strS或A list的int秒.有没有办法用Python做到这一点?就像是:
from typing import List
def my_function(list_arg: List[str|int]):
...
...
Run Code Online (Sandbox Code Playgroud) 我正在尝试根据PEP 484在Python 2中键入注释函数。该函数接受一个应同时实现__len__和的容器__iter__。我要在其中添加此批注的原始代码非常复杂,因此请考虑一个示例函数,如果该函数返回偶数,则返回int容器中所有s 的乘积,否则返回1。slen(s)
如果我想在只__len__需要一个容器的地方添加注释,我将其注释为type: (Sized) -> int。如果我想在只__iter__需要一个容器的地方添加注释,我将其注释为type: (Iterable[int]) -> int。但是,如何在需要两者的地方对容器进行完美注释?
我根据piotr-?wiek的建议进行了尝试:
from __future__ import print_function
from typing import Sized, Iterable
class SizedIterable(Sized, Iterable[int]):
pass
def product2(numbers):
# type: (SizedIterable) -> int
if len(numbers)%2 == 1:
return 1
else:
p = 1
for n in numbers:
p*= n
return p
print(product2([1, 2, 3, 4]))
print(product2({1, 2, 3, 4}))
Run Code Online (Sandbox Code Playgroud)
但这失败,并显示以下错误:
prod2.py:17: error: …Run Code Online (Sandbox Code Playgroud) 我发现Python打算支持静态类型,但它仍处于测试阶段.我用python 3.4.3尝试了下面的代码:
def somme(a: int, b: int) -> int:
return a + b
Run Code Online (Sandbox Code Playgroud)
支持语法,但我没有得到预期的结果.如果我输入somme('1', '3')我得到13,而我应该得到TypeError例外说int variable expected.
有谁知道它为什么不起作用?
为什么在设置或获取具有错误索引数的系列中的项时,pandas的行为会有所不同:
df = pd.DataFrame({'a': [10]})
# df['a'] is a series, can be indexed with 1 index only
# will raise IndexingError, as expected
df['a'].iloc[0, 0]
df['a'].loc[0, 0]
# will raise nothing, not as expected
df['a'].iloc[0, 0] = 1000 # equivalent to pass
df['a'].loc[0, 0] = 1000 # equivalent to df['a'].loc[0] = 1000
# pandas version 0.18.1, python 3.5
Run Code Online (Sandbox Code Playgroud)
编辑:报告.
python ×8
python-3.x ×4
pandas ×2
boost ×1
boost-graph ×1
c++ ×1
copy ×1
d3.js ×1
datetime ×1
deep-copy ×1
function ×1
generics ×1
javascript ×1
linux ×1
mypy ×1
performance ×1
process ×1
scikit-learn ×1
stack ×1
typing ×1
windows ×1