小编rah*_*hmu的帖子

常量指针与常量值上的指针

以下声明之间有什么区别?

char * const a;
const char * a;
Run Code Online (Sandbox Code Playgroud)

为了理解我写这个小程序的区别:

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


int main (int argc, char **argv)
{
    char a = 'x';
    char b = 'y';

    char * const pc1 = &a;
    const char * pc2 = &a;

    printf ("Before\n");
    printf ("pc1=%p\n", pc1);
    printf ("*pc1=%c\n", *pc1);
    printf ("pc2=%p\n", pc2);
    printf ("*pc2=%c\n", *pc2);

    *pc1 = b;
/*     pc1 = &b; */

/*     *pc2 = b; */
    pc2 = &b;

    printf ("\n\n");

    printf ("After\n");
    printf ("pc1=%p\n", pc1);
    printf ("*pc1=%c\n", …
Run Code Online (Sandbox Code Playgroud)

c pointers const

142
推荐指数
8
解决办法
16万
查看次数

Python argparse - 为多个子分析器添加参数

我的脚本定义了一个主解析器和多个子解析器.我想将这个-p论点应用于一些subparser.到目前为止代码看起来像这样:

parser = argparse.ArgumentParser(prog="myProg")
subparsers = parser.add_subparsers(title="actions")

parser.add_argument("-v", "--verbose",
                    action="store_true",
                    dest="VERBOSE",
                    help="run in verbose mode")

parser_create = subparsers.add_parser ("create", 
                                        help = "create the orbix environment")
parser_create.add_argument ("-p", 
                            type = int, 
                            required = True, 
                            help = "set db parameter")

# Update
parser_update = subparsers.add_parser ("update", 
                                        help = "update the orbix environment")
parser_update.add_argument ("-p", 
                            type = int, 
                            required = True, 
                            help = "set db parameter")
Run Code Online (Sandbox Code Playgroud)

如你所见,add_arument ("-p")重复两次.我实际上有更多的subsparsers.有没有办法循环遍历现有的子分析符以避免重复?

为了记录,我使用的是Python 2.7

python argparse

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

在Python2.x中使用print()(函数版本)

我理解语句和表达式之间的区别,我理解Python3将print()转换为函数.

但是我在各种Python2.x解释器上运行了一个用括号括起来的print()语句,它运行完美,我甚至不需要导入任何模块.

我的问题:以下代码print("Hello SO!")是否作为Python2.x中的语句或表达式进行了评估?

python printing python-2.x function-call

49
推荐指数
2
解决办法
5万
查看次数

Python格式表格输出

使用python2.7,我正在尝试打印到屏幕表格数据.

这大致是我的代码:

for i in mylist:
   print "{}\t|{}\t|".format (i, f(i))
Run Code Online (Sandbox Code Playgroud)

问题是,根据长度i或f(i)数据不会对齐.

这就是我得到的:

|foo |bar |
|foobo   |foobar  |
Run Code Online (Sandbox Code Playgroud)

我想得到什么:

|foo     |bar     |
|foobo   |foobar  |
Run Code Online (Sandbox Code Playgroud)

有没有允许这样做的模块?

python

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

睡在emacs lisp中

脚本A.

    (insert (current-time-string))
    (sleep-for 5)
    (insert (current-time-string))
Run Code Online (Sandbox Code Playgroud)

M-x eval-buffer,插入两个时间串,相隔5秒

脚本B.

一些comint代码(添加钩子,启动进程)

    (sleep-for 60) ;delay a bit for process to finish
    (insert "ZZZ")
Run Code Online (Sandbox Code Playgroud)

M-x eval-buffer,"ZZZ"立即插入,没有任何时间延迟

可能发生了什么?顺便说一句,它是Win XP上的Emacs 23.2

lisp emacs sleep

17
推荐指数
2
解决办法
3384
查看次数

Python:迭代构造函数的参数

我经常发现自己编写这样的类构造函数:

class foo:
    def __init__(self, arg1, arg2, arg3):
        self.arg1 = arg1
        self.arg2 = arg2
        self.arg3 = arg3
Run Code Online (Sandbox Code Playgroud)

如果参数(和类属性)的数量变高,这显然会变得很痛苦.我正在寻找循环构造函数的参数列表并相应地分配属性的最pythonic方法.我正在使用Python 2.7,所以理想情况下我正在寻找该版本的帮助.

python constructor arguments

13
推荐指数
2
解决办法
7425
查看次数

如何将整个文件加载到C中的字符串中

可能重复:
以C语言获取文件内容的最简单方法

我的程序读取跨越多行的文件.我想将文件的内容保存在一个字符串中.

在执行之前我不知道文件的行数,但是我已经将行大小修改为MA​​X_LINE_LEN.

你怎么能这样做?

c string file

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

无法在gdb中重现段错误

我在运行项目时遇到了段错误.每次我在gdb中运行程序时,段错误都会消失.这种行为不是随机的:每次我在shell中运行它都会出现段错误,每次我在gdb中运行它时,段错误都会消失.(我确实使用-g重新编译).

所以在我开始在代码中的任何地方疯狂添加printfs之前,我想知道一些事情:

  • 这种行为常见吗?
  • 解决这个问题的最佳方法是什么?

我不知道测试是否可以编写脚本,因为我的应用程序是交互式的,并且在特定的用户输入上崩溃.

我没有在这里粘贴我的代码,因为它太长了.但如果有人有兴趣帮忙,请点击这里:https: //github.com/rahmu/Agros

c gdb

12
推荐指数
1
解决办法
5205
查看次数

数据如何在多个调饰函数调用中保持持久性?

以下函数旨在用作存储已计算值的结果的装饰器.如果之前已经计算过该参数,该函数将返回存储在cache字典中的值:

def cached(f):
    f.cache = {}
    def _cachedf(*args):
        if args not in f.cache:
            f.cache[args] = f(*args)

        return f.cache[args]

    return _cachedf
Run Code Online (Sandbox Code Playgroud)

我意识到(错误地)cache不需要是函数对象的属性.事实上,以下代码也适用:

def cached(f):
    cache = {}   # <---- not an attribute this time!
    def _cachedf(*args):
        if args not in cache:
            cache[args] = f(*args)

        return cache[args]
    return _cachedf
Run Code Online (Sandbox Code Playgroud)

我很难理解cache对象如何在多个调用中持久化.我尝试多次调用多个缓存函数,但找不到任何冲突或问题.

任何人都可以帮助我理解cache即使在_cachedf函数返回后变量仍然存在?

python internals decorator memoization

6
推荐指数
1
解决办法
2052
查看次数

为什么+和*分别评估为0和1?

我正在使用GNU/MIT Scheme:

1 ]=> (+)

;Value: 0

1 ]=> (*)

;Value: 1

1 ]=> (-)

;The procedure #[arity-dispatched-procedure 2] has been called with 0 arguments; it requires at least 1 argument.
;To continue, call RESTART with an option number:
; (RESTART 1) => Return to read-eval-print level 1.

2 error> (/)

;The procedure #[arity-dispatched-procedure 3] has been called with 0 arguments; it requires at least 1 argument.
;To continue, call RESTART with an option number:
; (RESTART 2) => Return …
Run Code Online (Sandbox Code Playgroud)

lisp scheme

6
推荐指数
1
解决办法
147
查看次数