>>> def hehe():
... return "spam"
...
>>> repr(hehe)
'<function hehe at 0x7fe5624e29b0>'
Run Code Online (Sandbox Code Playgroud)
我希望有:
>>> repr(hehe)
'hehe function created by awesome programmer'
Run Code Online (Sandbox Code Playgroud)
我怎么做?把__repr__
内部hehe
功能不起作用.
编辑:
如果你们想知道我为什么要这样做:
>>> defaultdict(hehe)
defaultdict(<function hehe at 0x7f0e0e252280>, {})
Run Code Online (Sandbox Code Playgroud)
我只是不喜欢它在这里显示的方式.
>>> import sys
>>> sys.getfilesystemencoding()
'UTF-8'
Run Code Online (Sandbox Code Playgroud)
我该如何改变?我知道如何更改默认的系统编码.
>>> reload(sys)
<module 'sys' (built-in)>
>>> sys.setdefaultencoding('ascii')
Run Code Online (Sandbox Code Playgroud)
但是没有sys.setfilesystemencoding.
converter_scientific_notation_to_decimal_notation('1.34E-15')或converter_scientific_notation_to_decimal_notation(1.34E-15)
>'0.00000000000000134'
converter_scientific_notation_to_decimal_notation('2.54E-20')或converter_scientific_notation_to_decimal_notation(2.54E-20)
>'0.000000000000000000254'
Javascript中是否存在此类函数?
parseFloat不适合大的负面科学数字.
parseFloat('1.34E-5') => 0.0000134
parseFloat('1.34E-15') => 1.34e-15
Run Code Online (Sandbox Code Playgroud) python的单元测试库(特别是3.x,我真的不关心2.x)是否只有root用户才能访问装饰器?
我有这个测试功能.
def test_blabla_as_root():
self.assertEqual(blabla(), 1)
Run Code Online (Sandbox Code Playgroud)
blabla函数只能由root执行.我只想要root用户装饰器,所以普通用户将跳过此测试:
@support.root_only
def test_blabla_as_root():
self.assertEqual(blabla(), 1)
Run Code Online (Sandbox Code Playgroud)
这样的装饰是否存在?我们有@ support.cpython_only装饰器.
我正在调试Python源代码(CPython 3.4).我正在收到PyObject*.我想printf
.
例如(来自Python 3.4的Objects/floatobject.c源代码):
PyObject *o_ndigits = NULL;
Py_ssize_t ndigits;
x = PyFloat_AsDouble(v);
if (!PyArg_ParseTuple(args, "|O", &o_ndigits))
return NULL;
Run Code Online (Sandbox Code Playgroud)
我如何printf
o_ndigits?它与这个问题几乎相同:Python:获取PyObject的字符串表示形式?,但对于Python3.使用原始解决方案,我收到了"undefined reference to PyString_AsString"
错误.
所以我根据本指南训练了初始模型以识别花朵.https://www.tensorflow.org/versions/r0.8/how_tos/image_retraining/index.html
bazel build tensorflow/examples/image_retraining:retrain
bazel-bin/tensorflow/examples/image_retraining/retrain --image_dir ~/flower_photos
Run Code Online (Sandbox Code Playgroud)
要通过命令行对图像进行分类,我可以这样做:
bazel build tensorflow/examples/label_image:label_image && \
bazel-bin/tensorflow/examples/label_image/label_image \
--graph=/tmp/output_graph.pb --labels=/tmp/output_labels.txt \
--output_layer=final_result \
--image=$HOME/flower_photos/daisy/21652746_cc379e0eea_m.jpg
Run Code Online (Sandbox Code Playgroud)
但是如何通过Tensorflow服务提供此图表?
关于设置Tensorflow服务的指南(https://tensorflow.github.io/serving/serving_basic)没有说明如何合并图形(output_graph.pb).服务器需要不同格式的文件:
$>ls /tmp/mnist_model/00000001
checkpoint export-00000-of-00001 export.meta
Run Code Online (Sandbox Code Playgroud) >>> import types
>>> class Foo:
... def say(self):
... print("Foo say")
...
>>> class Bar:
... def say(self):
... print("Bar say")
...
>>> f = Foo()
>>> b = Bar()
>>> types.MethodType(f.say, b)()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: say() takes 1 positional argument but 2 were given
Run Code Online (Sandbox Code Playgroud)
我只是想知道我给出的 2 个参数是什么?我知道其中一个是self
,但另一个是什么?
当然,在这个例子中,正确的方法是:
>>> types.MethodType(Foo.say, b)()
Foo say
Run Code Online (Sandbox Code Playgroud)
但我问的是types.MethodType(f.say, b)()
. 我想知道为什么它会抱怨
需要 1 个位置参数,但给出了 2 个
class a_class:
def __getattr__(self, name):
# if called by hasattr(a, 'b') not by a.b
# print("I am called by hasattr")
print(name)
a = a_class()
a.b_attr
hasattr(a, 'c_attr')
Run Code Online (Sandbox Code Playgroud)
请看看里面的评论__getattr__
.我怎么做?我正在使用Python 3.原因是我想动态创建属性但我不想在使用hasattr时这样做.谢谢.
我允许删除类中的某些属性.但是,其他人被禁止删除.
class klass:
a = 1
b = 2
def __delattr__(cls, attr):
if attr=='a':
pass # go ahead, delete attribute a
elif attr=='b':
raise TypeError("Bad boy/girl, you shouldn't delete attribute b")
del klass.a
del klass.b
Run Code Online (Sandbox Code Playgroud)
此代码不起作用.代码有什么问题?这两个属性仍在删除.我顺便使用Python 3.__delattr__
似乎不起作用.请注意,我不想实例化该类(我不想要foo = klass(); del foo.a;
/我想要del klass.a;
).谢谢.
python ×7
python-3.x ×2
encoding ×1
filesystems ×1
javascript ×1
python-c-api ×1
tensorflow ×1