小编bru*_*yne的帖子

Python装饰器保持签名和用户定义的属性

我有我的简单装饰器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)

python ipython python-2.7 python-decorators

8
推荐指数
2
解决办法
882
查看次数

元素的角度绑定类到元素焦点

在我的下面的角度应用程序中,我有多行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)

html javascript css angularjs

7
推荐指数
1
解决办法
1208
查看次数

python set union 操作对于命名元组表现不佳

我想在 python 中创建一组namedtuple,能够使用联合操作动态添加元素。

下面的代码片段创建了一个setof 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并将其添加到我的操作setunion,它的行为将不会按预期进行。

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 都显示出相同的行为。

python set namedtuple python-collections

4
推荐指数
1
解决办法
779
查看次数

记录器为什么没有记录自己的名字?

我试图复制以下代码段中发生的情况.在这里,我有两个不同的记录器尝试记录一些信息.

每个都配置为将日志记录信息打印到控制台.代码的问题是它没有在消息之前打印记录器的名称,这里出了什么问题?

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)

python logging

0
推荐指数
1
解决办法
51
查看次数

为什么内存泄漏在一个案例而不是另一个案件中

我正在创建一个c++具有两种略有不同的方式的对象,在以下代码CASE0存在内存泄漏,但在这种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 0valgrind说有内存泄漏

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)

c++ valgrind memory-leaks

0
推荐指数
1
解决办法
108
查看次数