如果我有一个实用工具函数foo,我希望能够从我的ng-app声明中的任何地方调用.有没有我可以在我的模块设置中使其全局可访问,或者我是否需要将其添加到每个控制器的范围?
我常常发现自己对如何使用术语"参数"和"参数"感到困惑.它们似乎在编程世界中可以互换使用.
使用它们的正确惯例是什么?
我有一个int arr[5]传递给函数的数组fillarr(int arr[]):
int fillarr(int arr[])
{
for(...);
return arr;
}
Run Code Online (Sandbox Code Playgroud)
我想从两个单独的变量中的函数返回两个值.例如:
def select_choice():
loop = 1
row = 0
while loop == 1:
print('''Choose from the following options?:
1. Row 1
2. Row 2
3. Row 3''')
row = int(input("Which row would you like to move the card from?: "))
if row == 1:
i = 2
card = list_a[-1]
elif row == 2:
i = 1
card = list_b[-1]
elif row == 3:
i = 0
card = list_c[-1]
return i
return card
Run Code Online (Sandbox Code Playgroud)
我希望能够分别使用这些值.当我尝试使用时return i, card,它返回一个tuple …
是否可以将带有参数的函数传递给Python中的另一个函数?
比如说:
def perform(function):
return function()
Run Code Online (Sandbox Code Playgroud)
但要传递的函数将具有如下参数:
action1()
action2(p)
action3(p,r)
Run Code Online (Sandbox Code Playgroud) 一般来说,我们都听说过编程语言中的函数或过程.但是,我发现我几乎可以互换地使用这些术语(这可能是非常错误的).
所以,我的问题是:
它们的功能,用途和用途有何不同?
一个例子将不胜感激.
没有多少人知道这个功能,但Python的功能(和方法)可以有属性.看吧:
>>> def foo(x):
... pass
...
>>> foo.score = 10
>>> dir(foo)
['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__get__', '__getattribute__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name', 'score']
>>> foo.score
10
>>> foo.score += 1
>>> foo.score
11
Run Code Online (Sandbox Code Playgroud)
Python中此功能的可能用途和滥用情况是什么?我知道的一个好用途是PLY使用docstring将语法规则与方法相关联.但是自定义属性呢?有充分的理由使用它们吗?
我有一个用PHP编写的代码片段,它从数据库中提取一个文本块并将其发送到网页上的小部件.原始文本块可以是冗长的文章或短句或短句; 但对于这个小部件,我不能显示超过200个字符.我可以使用substr()来切断200个字符的文本,但结果会在单词中间切断 - 我真正想要的是在200个字符之前在最后一个单词的末尾剪切文本.
我发现C#中的命名参数功能在某些情况下非常有用.
calculateBMI(70, height: 175);
Run Code Online (Sandbox Code Playgroud)
如果我想在javascript中使用该怎么办?
我不想要的是 -
myFunction({ param1: 70, param2: 175 });
function myFunction(params){
// Check if params is an object
// Check if the parameters I need are non-null
// Blah blah
}
Run Code Online (Sandbox Code Playgroud)
我已经使用过这种方法.还有另外一种方法吗?
我可以使用任何库来做这件事.(或者有人可以指出我已经做过的那个)
R语言有一个很好的功能,用于定义可以采用可变数量参数的函数.例如,该函数data.frame接受任意数量的参数,并且每个参数都成为结果数据表中列的数据.用法示例:
> data.frame(letters=c("a", "b", "c"), numbers=c(1,2,3), notes=c("do", "re", "mi"))
letters numbers notes
1 a 1 do
2 b 2 re
3 c 3 mi
Run Code Online (Sandbox Code Playgroud)
函数的签名包括省略号,如下所示:
function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,
stringsAsFactors = default.stringsAsFactors())
{
[FUNCTION DEFINITION HERE]
}
Run Code Online (Sandbox Code Playgroud)
我想编写一个类似的函数,获取多个值并将它们合并为一个返回值(以及进行一些其他处理).为了做到这一点,我需要弄清楚如何...从函数中的函数参数"解包" .我不知道该怎么做.功能定义中的相关行data.frame是object <- as.list(substitute(list(...)))[-1L],我无法理解.
那么如何将省略号从函数的签名转换为例如列表呢?
更具体地说,我如何写get_list_from_ellipsis下面的代码?
my_ellipsis_function(...) {
input_list <- get_list_from_ellipsis(...)
output_list <- lapply(X=input_list, FUN=do_something_interesting)
return(output_list)
}
my_ellipsis_function(a=1:10,b=11:20,c=21:30)
Run Code Online (Sandbox Code Playgroud)
似乎有两种可能的方法来做到这一点.他们是as.list(substitute(list(...)))[-1L]和list(...).但是,这两者并没有完全相同.(有关差异,请参阅答案中的示例.)任何人都可以告诉我它们之间的实际区别是什么,我应该使用哪一个?
function ×10
python ×3
parameters ×2
return ×2
terminology ×2
angularjs ×1
arguments ×1
arrays ×1
attributes ×1
c++ ×1
ellipsis ×1
global ×1
javascript ×1
list ×1
php ×1
pointers ×1
procedure ×1
r ×1
return-value ×1
string ×1
variadic ×1