我在以下方面使用strcmp
将指针传递给字符串文字,但第二个结果是seg错误.即使我已经确认指针指向正确的字符串文字,我很困惑为什么我得到seg错误..这是代码: -
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char const *args[])
{
char firstName[strlen(*++args)];
strcpy(firstName, *args);
char lastName[strlen(*++args)];
strcpy(lastName, *args);
printf("%s\t%s\n", firstName, lastName);
printf("%d\n", strcmp(firstName, lastName));// this works
printf("%d\n", strcmp(*(--args),*(++args)));//this gives me a seg fault
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)我将它保存为str.c,当我编译它时,首先我得到以下警告:
[Neutron@Discovery examples]$ gcc -Wall str.c -o str
str.c: In function ‘main’:
str.c:15: warning: operation on ‘args’ may be undefined
Run Code Online (Sandbox Code Playgroud)
最后运行它,给出一个seg错误,如下所示
[Neutron@Discovery examples]$ ./str Jimmy Neutron
Jimmy Neutron
-4
Segmentation fault (core dumped)
Run Code Online (Sandbox Code Playgroud) 可能重复:
python*运算符的专有名称?
*拆包当清楚地使用的参数的任意数量和
这是传统的使用*args和**kwargs,我倾向于这些发音为"ARGS"和"quargs",忽略了*秒.
在这个问题中,星号被称为指针.
BDFL如何引用这些符号?
感谢您找到相关问题,这就是我所寻求的.准确地搜索(甚至非常特定的)网络真的很棘手*!
对那些回答他们如何发音"*"的评论员来说,这不是我问的问题(如果我的问题误导了你,我会道歉).我想知道的是这些说明符是如何在Python中命名的,这不是一个主观问题(因此引用了BDFL).
好吧,我已经尝试了很长一段时间.我不能将args和kwargs传递到django app中的视图吗?我是否必须独立定义每个关键字参数?
例如,
#views.py
def someview(request, *args, **kwargs):
...
Run Code Online (Sandbox Code Playgroud)
在调用视图时,
response = someview(request,locals())
Run Code Online (Sandbox Code Playgroud)
我似乎无法做到这一点.相反,我必须这样做:
#views.py
def someview(request, somekey = None):
...
Run Code Online (Sandbox Code Playgroud)
有什么原因吗?
这就是我要找的东西:
def __init__(self, *args):
list_of_args = #magic
Parent.__init__(self, list_of_args)
Run Code Online (Sandbox Code Playgroud)
我需要将*args传递给单个数组,以便:
MyClass.__init__(a, b, c) == Parent.__init__([a, b, c])
Run Code Online (Sandbox Code Playgroud) 我一直在谷歌搜索近一个小时,我只是卡住了.
对于脚本stupidadder.py,它为命令arg添加2.
例如python stupidadder.py 4
打印6
python stupidadder.py 12
打印14
到目前为止我用谷歌搜索过:
import argparse
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('x', metavar='x', type=int, nargs='+',
help='input number')
...
args = parser.parse_args()
print args
x = args['x'] # fails here, not sure what to put
print x + 2
Run Code Online (Sandbox Code Playgroud)
我无法在任何地方找到直截了当的答案.文档太混乱了.:(有人可以帮忙吗?请和谢谢.:)
python 中 *args 和 **kwargs 的用法对我来说很清楚,并且 SO 中存在很多问题(例如*args 和 **kwargs 的使用以及**(双星/星号)和 *(星号/星号)对参数做什么?)。
但我想理解的一件事是:为什么不能同时定义强制位置参数、强制 kwarg 参数并最终仍然允许捕获其他参数和 kwargs,如下所示cant_do_that?
def one_kwarg_is_mandatory(*, b, **kwargs):
print(b)
for key, value in kwargs.items():
print(key, value)
def one_pos_arg_and_one_kwarg_are_mandatory(a, *, b, **kwargs):
print(a, b)
for key, value in kwargs.items():
print(key, value)
# I wanted a mandatory arg (a) and possibly parse other args (*args),
# then a mandatory kwarg (b) and eventually other kwargs (**kwargs)
def cant_do_that(a, *args, *, b, **kwargs):
print(a, b) …Run Code Online (Sandbox Code Playgroud) 它是如何在引擎盖下工作的?我不明白以下错误的原因:
>>> def f():
... yield 1,2
... yield 3,4
...
>>> *f()
File "<stdin>", line 1
*f()
^
SyntaxError: invalid syntax
>>> zip(*f())
[(1, 3), (2, 4)]
>>> zip(f())
[((1, 2),), ((3, 4),)]
>>> *args = *f()
File "<stdin>", line 1
*args = *f()
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud) 您将如何运行命令并使用Flutter / Dart传递一些自定义参数,以便随后可以在main()调用中访问它们,例如:
flutter run -device [my custom arg]
Run Code Online (Sandbox Code Playgroud)
因此,我可以通过以下方式访问它:
void main(List<String> args) {
print(args.toString());
}
Run Code Online (Sandbox Code Playgroud)
谢谢。
这是 Google 风格文档字符串的摘录:
def function_with_types_in_docstring(param1, param2):
"""Example function with types documented in the docstring.
`PEP 484`_ type annotations are supported. If attribute, parameter, and
return types are annotated according to `PEP 484`_, they do not need to be
included in the docstring:
Args:
param1 (int): The first parameter.
param2 (str): The second parameter.
Returns:
bool: The return value. True for success, False otherwise.
.. _PEP 484:
https://www.python.org/dev/peps/pep-0484/
"""
Run Code Online (Sandbox Code Playgroud)
如果描述跨越一行,如何格式化参数?
main我是 C# 世界的新手,但看到其他编程语言可以从函数/方法sys.argv等地方访问命令行参数。
在Visual Studio 的样板 ASP.NET Core Web App MVC(版本 6.0)文件中看到以下语句是不寻常的:Program.cs
var builder = WebApplication.CreateBuilder(args);
Run Code Online (Sandbox Code Playgroud)
将鼠标悬停在 IDE 内的该参数上似乎表明确实如此string[] args,但是入口点在哪里?
args ×10
python ×7
kwargs ×2
arguments ×1
arrays ×1
asp.net-mvc ×1
boilerplate ×1
c ×1
c# ×1
command ×1
dart ×1
django ×1
django-views ×1
docstring ×1
entry-point ×1
evaluation ×1
flutter ×1
naming ×1
parsing ×1
python-2.7 ×1
python-3.x ×1
strcmp ×1
styles ×1