小编Ank*_*wal的帖子

Unicode可打印字符的范围是多少?

任何人都可以告诉我Unicode可打印字符的范围是什么?[例如Ascii可打印字符范围是\ u0020 - \u007f]

unicode character-encoding unicode-string

45
推荐指数
5
解决办法
3万
查看次数

python函数默认参数只评估一次?

我是一个python初学者,阅读'python tutorial',它说如果我们有一个函数:

def f(a, L=[]):
     L.append(a)
     return L
print f(1)
print f(2)
print f(3)
Run Code Online (Sandbox Code Playgroud)

这将打印

[1]
[1, 2]
[1, 2, 3]
Run Code Online (Sandbox Code Playgroud)

因为默认值只计算一次而list是可变对象.我能理解.

它说继续,如果我们不希望在后续调用之间共享默认值,我们可以:

def f(a, L=None):
   if L is None:           #line  2
       L = []            
   L.append(a)
   return L
print f(1)            
print f(2)
print f(3)
Run Code Online (Sandbox Code Playgroud)

这将输出:

[1]
[2]
[3]
Run Code Online (Sandbox Code Playgroud)

为什么呢?怎么解释这个.我们知道默认值只是被评估once,当我们调用f(2)时,L不是None而且if(第2行)不能为真,所以L.append(a)== [1,2].我可以猜出由于某种原因再次评估默认值,但是什么是'某种原因',只是因为python解释器看到了if L is None: L = []

python parameters function

30
推荐指数
3
解决办法
4908
查看次数

编写一个bash shell脚本,在用户定义的时间内消耗一定量的RAM

我正在尝试编写一个bash shell脚本,该脚本在用户定义的时间内在嵌入式设备上消耗大量RAM.如何在不使用数组的情况下执行此操作?

linux bash shell embedded-linux

24
推荐指数
1
解决办法
3万
查看次数

C struct hack at work

这是如何使用C struct hack时分配的"额外"内存?

问题:

我在下面有一个C struct hack实现.我的问题是如何使用我分配给hack的"额外"内存.有人可以给我一个使用额外内存的例子吗?

#include<stdio.h>
#include<stdlib.h>

int main()
{

    struct mystruct {

        int len;
        char chararray[1];

    };

    struct mystruct *ptr = malloc(sizeof(struct mystruct) + 10 - 1);
    ptr->len=10;


    ptr->chararray[0] = 'a';
    ptr->chararray[1] = 'b';
    ptr->chararray[2] = 'c';
    ptr->chararray[3] = 'd';
    ptr->chararray[4] = 'e';
    ptr->chararray[5] = 'f';
    ptr->chararray[6] = 'g';
    ptr->chararray[7] = 'h';
    ptr->chararray[8] = 'i';
    ptr->chararray[9] = 'j';


}
Run Code Online (Sandbox Code Playgroud)

c

21
推荐指数
2
解决办法
7218
查看次数

方法对象与函数对象,Python类实例与类

我正在尝试验证实例属性和类属性之间的区别,如2012年11月1日的Python教程版本2.7.3,第9章:类,最后一行(源代码)所示:

实例对象的有效方法名称取决于其类.根据定义,作为函数对象的类的所有属性都定义其实例的相应方法.所以在我们的例子中,xf是一个有效的方法引用,因为MyClass.f是一个函数,但是xi不是,因为MyClass.i不是.但是xf与MyClass.f不同 - 它是一个方法对象,而不是一个函数对象.

我有这个:

class MyClass:    
   """A simple example class"""    
   i = 12345   
   def f():    
      return 'hello world'
Run Code Online (Sandbox Code Playgroud)

然后我这样做:

>>> x = MyClass()
>>> x.f
<bound method MyClass.f of <__main__.MyClass instance at 0x02BB8968>>
>>> MyClass.f
<unbound method MyClass.f>
>>> type(MyClass.f)
<type 'instancemethod'>
>>> type(x.f)
<type 'instancemethod'>
Run Code Online (Sandbox Code Playgroud)

需要注意的是这两种类型x.fMyClass.f为instancemethod.类型没有区别,但教程另有说法.有人可以澄清一下吗?

python methods class instance

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

为什么这个正则表达式测试不起作用?

我有这个

echo $line
Thisisaline.
Run Code Online (Sandbox Code Playgroud)

我想知道为什么这不起作用:

if [[ "$line" =~ "[a-zA-Z]+\.$" ]] ; then echo "hello"; fi
Run Code Online (Sandbox Code Playgroud)

以上正则表达式没有输出.

regex bash shell

19
推荐指数
3
解决办法
4万
查看次数

如何在Linux中获得等效的/ dev/one

您可以使用

dd if=/dev/zero of=file count=1024 bs=1024 
Run Code Online (Sandbox Code Playgroud)

零填充文件.

而不是我想要填写一个文件.我怎么做?

没有/ dev/one文件,那么如何通过bash shell模拟该效果呢?

linux bash file

19
推荐指数
3
解决办法
1万
查看次数

为什么ps o/p在管道之后列出grep进程?

当我做

$ ps -ef | grep cron
Run Code Online (Sandbox Code Playgroud)

我明白了

root      1036     1  0 Jul28 ?        00:00:00 cron
abc    21025 14334  0 19:15 pts/2    00:00:00 grep --color=auto cron
Run Code Online (Sandbox Code Playgroud)

我的问题是为什么我会看到第二行.根据我的理解,ps列出进程并将列表管道grep.grepps列出流程时甚至没有开始运行,那么如何grep在o/p中列出流程?

相关第二个问题:

当我做

$ ps -ef | grep [c]ron
Run Code Online (Sandbox Code Playgroud)

我只得到

root      1036     1  0 Jul28 ?        00:00:00 cron
Run Code Online (Sandbox Code Playgroud)

第一次和第二次grep执行有什么区别?

linux bash pipe ps

17
推荐指数
3
解决办法
6585
查看次数

bash中的代码块使用{}

我想知道为什么在下面的例子中使用了代码块:

possibly_hanging_job & { sleep ${TIMEOUT}; eval 'kill -9 $!' &> /dev/null; }
Run Code Online (Sandbox Code Playgroud)

这可能是这样编写的(不使用代码块).. right?

possibly_hanging_job & 
sleep ${TIMEOUT}
eval 'kill -9 $!' &> /dev/null
Run Code Online (Sandbox Code Playgroud)

bash shell

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

变量名称之后的' - '(短划线)在这里做什么?

if [ -n "${BASH-}" -o -n "${ZSH_VERSION-}" ] ; then
    hash -r 2>/dev/null
fi
Run Code Online (Sandbox Code Playgroud)

我在哪里可以找到关于此的参考?谢谢.

linux bash

15
推荐指数
2
解决办法
1761
查看次数