我有我的简单装饰器my_decorator
装饰my_func
.
def my_decorator(func):
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
wrapper._decorator_name_ = 'my_decorator'
return wrapper
@my_decorator
def my_func(x):
print('hello %s'%x)
my_func._decorator_name_
'my_decorator'
Run Code Online (Sandbox Code Playgroud)
直到这里工作,但我看不到功能的实际签名.
my_func?
Signature: my_func(*args, **kwargs)
Docstring: <no docstring>
File: ~/<ipython-input-2-e4c91999ef66>
Type: function
Run Code Online (Sandbox Code Playgroud)
如果我用python's装饰我的装饰器decorator.decorator
,我可以看到我的函数的签名但我不能拥有我定义的新属性.
import decorator
@decorator.decorator
def my_decorator(func):
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
wrapper._decorator_name_ = 'my_decorator'
return wrapper
@my_decorator
def my_func(x):
print('hello %s'%x)
my_func?
Signature: my_func(x)
Docstring: <no docstring>
File: ~/<ipython-input-8-934f46134434>
Type: function
my_func._decorator_name_
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-10-7e3ef4ebfc8b> …
Run Code Online (Sandbox Code Playgroud) 在我的下面的角度应用程序中,我有多行myelement
(角度指令包装器在input
标签上).我需要集中/选择/突出其中一个,.selected
样式中的类就是这样.
在下面的应用程序中,一切正常,除了专注于input
标记,需要由css类限制selected
.IE中任何具有类selected
的元素都input
应该关注相应的标记.我该如何解决这个问题?
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<style>
.container {
display: flex;
flex-direction: column;
width: 600px;
}
.notebook {
display: flex;
justify-content: center;
}
.cell {
margin: 5px;
padding: 5px;
}
.selected {
border-style: solid;
border-color: green;
border-width: 1px;
border-left-width: 5px;
}
</style>
</head>
<body ng-app="myApp">
<div ng-controller="ListController as listctrl" class="notebook">
<div class="container">
<myelement ng-repeat="i in …
Run Code Online (Sandbox Code Playgroud) 我想在 python 中创建一组namedtuple
,能够使用联合操作动态添加元素。
下面的代码片段创建了一个set
of namedtuple
,它表现得很好。
from collections import namedtuple
B = namedtuple('B', 'name x')
b1 = B('b1',90)
b2 = B('b2',92)
s = set([b1,b2])
print(s)
Run Code Online (Sandbox Code Playgroud)
打印
{B(name='b1', x=90), B(name='b2', x=92)}
Run Code Online (Sandbox Code Playgroud)
现在,如果我创建另一个namedtuple
并将其添加到我的操作set
中union
,它的行为将不会按预期进行。
b3 = B('b3',93)
s = s.union(b3)
print(s)
Run Code Online (Sandbox Code Playgroud)
该代码片段打印以下输出。
{93, B(name='b1', x=90), B(name='b2', x=92), 'b3'}
Run Code Online (Sandbox Code Playgroud)
预期输出应该是:
{B(name='b1', x=90), B(name='b2', x=92), B(name='b3', x=93)}
Run Code Online (Sandbox Code Playgroud)
我是不是对API理解有误?python2 和 3 都显示出相同的行为。
我试图复制以下代码段中发生的情况.在这里,我有两个不同的记录器尝试记录一些信息.
每个都配置为将日志记录信息打印到控制台.代码的问题是它没有在消息之前打印记录器的名称,这里出了什么问题?
import logging
import sys
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
logger1 = logging.getLogger("logger1")
logger1.setLevel(logging.DEBUG)
logger1.addHandler(ch)
logger2 = logging.getLogger("logger2")
logger2.setLevel(logging.DEBUG)
logger2.addHandler(ch)
logger1.debug('log statement')
logger2.debug('log statement')
Run Code Online (Sandbox Code Playgroud)
我得到的输出是,
log statement
log statement
Run Code Online (Sandbox Code Playgroud)
我期待的是
logger1: log statement
logger2: log statement
Run Code Online (Sandbox Code Playgroud) 我正在创建一个c++
具有两种略有不同的方式的对象,在以下代码CASE
中0
存在内存泄漏,但在这种else
情况下没有内存泄漏.
#include <string>
#define CASE 1
class A {
private:
std::string *s;
public:
A(std::string *p_s) { s = p_s; }
};
int main() {
#if CASE==0
auto a = A(new std::string("Hello"));
#else
auto s = std::string("Hello");
auto a = A(&s);
#endif
}
Run Code Online (Sandbox Code Playgroud)
当我设置CASE 0
了valgrind
说有内存泄漏
valgrind ./a.out
==24351== Memcheck, a memory error detector
==24351== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==24351== Using Valgrind-3.13.0 and …
Run Code Online (Sandbox Code Playgroud) python ×3
angularjs ×1
c++ ×1
css ×1
html ×1
ipython ×1
javascript ×1
logging ×1
memory-leaks ×1
namedtuple ×1
python-2.7 ×1
set ×1
valgrind ×1