我想编写一个带有参数的方法,默认为成员变量,如下所示:
def method(self, arg1=0, arg2=self.member):
Run Code Online (Sandbox Code Playgroud)
显然这是不允许的.我应该以不同的方式编写它,或者使用值arg2来表示何时使用成员变量?
我正在写一个C程序,我使用6个变量a,b,c,d,e,f
a,b,c是常量值,我应该从命令行作为参数传递.
d,e,f将是结构数组的大小.
typedef struct
{
blah blah
} ex;
ex ex0[d];
Run Code Online (Sandbox Code Playgroud)
我对如何将所有这些作为参数传递感到非常困惑.现在我已经硬编码了这些值,显然我不应该这样做.
我正在开发一个iPhone应用程序,并在方法中收到警告:
NSNumber *latitudeValue;
NSNumber *longitudeValue;
[self obtainLatitude:latitudeValue longitude:longitudeValue];
Run Code Online (Sandbox Code Playgroud)
该方法声明如下:
- (void) obtainLatitude:(NSNumber *)latitudeValue longitude:(NSNumber *)longitudeValue {
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
latitudeValue = [f numberFromString:[latitude.text stringByReplacingOccurrencesOfString:@"," withString:@"."]];
longitudeValue = [f numberFromString:[longitude.text stringByReplacingOccurrencesOfString:@"," withString:@"."]];
[f release];
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我正在尝试计算latitudeValue和longitudeValue调用,obtainLatitude:longitude:但我做错了.
我该如何解决这个错误?
我从C中断了一下,我又回到了原点.
如果我想创建一个二维的二维数组,我可以用两种方式做到:
double** m_array = (double**) malloc(2*sizeof(double*));
double* m_array = (double*) malloc(2*sizeof(double));
Run Code Online (Sandbox Code Playgroud)
要么
double array[2][2];
Run Code Online (Sandbox Code Playgroud)
但是,当我希望传递malloc'd数组而不是传递另一个时,似乎有两个约定:
//allowed for passing in malloc'd array, but not for other array
func_m(m_array) //allowed
func_m(array) //disallowed
func_m(double** m_array)
//allowed for passing in either array; required for passing in non-malloc'd array
func(m_array) //allowed
func(array) //allowed
func(double array[][2])
Run Code Online (Sandbox Code Playgroud)
在第一个中,我不需要任何信息,除了它是一个指针数组的指针.但它只能是一个malloc阵列.
在第二个中,我需要传递double*指向的数组的每个数组的长度.这看起来很傻.
我错过了什么吗?提前致谢.
我是Ruby的新手,需要编写一个可以处理多个输入文件的脚本.它应该像这样调用:
script.rb -i file*
Run Code Online (Sandbox Code Playgroud)
目录包含多个文件,例如file1.xml,file2.xml等等.只是一个简单的问题:这个通配符将如何扩展?我需要在我的脚本中编程吗?我正在使用OptionParserClass来解析命令行参数.
谢谢!
什么是[函数]参数?它们用于什么?
我最近开始学习Python; 我是编程的新手,我为这个基本问题道歉.
在每个Python教程中,我都会讨论参数.我已经找到了这个问题的答案,并找到了很多答案,但对我来说,这些答案有点难以理解.我可能只是缺少一些概念背景.
所以...当我定义一个函数时,括号中的东西用于什么?例:
def hi( This is the part that i dont get):
print 'hi'
Run Code Online (Sandbox Code Playgroud)
编辑:
与此相关的两个后续问题后来在这里被关闭和合并,因此部分答案的部分脱离背景特征.
后续问题是:[转述]
我正在尝试使用link_to将变量从视图传递到控制器.以下是我在视图中的代码:
<%= link_to 'Send Chant', :controller => 'send_chants', :action => 'index', :content => @chant.content %></b>
Run Code Online (Sandbox Code Playgroud)
这是我的'send_chants'控制器中的索引操作的样子:
class SendChantsController < ApplicationController
def index(content)
puts content
end
end
Run Code Online (Sandbox Code Playgroud)
点击"发送聊天"链接后,我得到一个ArgumentError错误的参数数量(0表示1)
我确定我错过了一些简单的事情.有什么想法吗?
非常感谢!
我正在编写一个计算最大高度/宽度的函数,并使元素具有相同的高度/宽度.我想将2个参数传递给此函数:
1. The selector, which can be(element, class )
2. jQuery `width()` / `height()` function
Run Code Online (Sandbox Code Playgroud)
并且期望的结果应该是看起来像的功能..
Eg 1. equalDimensions('.class', width() ); OR
Eg 2. equalDimensions( div, height() );
Run Code Online (Sandbox Code Playgroud)
并根据传递给函数的内容width()/ height()它将计算最大高度/宽度,并使所有作为第一个参数传递的选择器等于宽度/高度.
作为jQuery的新手,我正在努力将其放入代码中.
这是我到目前为止所尝试的......
var equalDimension = function( selector, fn ){
var dimension = fn();
var biggestDimemsion = 0;
$(selector).each(function(){
if($(this).dimension > biggestDimemsion){
biggestDimemsion = $(this).dimension;
}
});
$(selector).each(function(){
$(this).dimension(biggestDimemsion);
});
};
Run Code Online (Sandbox Code Playgroud) 我正在尝试实现插入排序.当我在while循环中编写swap方法时,该算法工作正常.但是当我尝试调用swap()函数时,它给出了错误的答案.具体来说,当我传递参数'j'时,它会使答案出错.你能告诉我我犯了哪个错误.(关于ideone![ http://ideone.com/MoqHgn])
#include <iostream>
using namespace std;
void swap(int *x, int *y, int *j) {
int temp = *x;
*x = *y;
*y = temp;
*j--;
}
int main() {
int N;
scanf("%d", &N);
int a[N];
for(int i = 0; i < N; i++) {
scanf("%d", &a[i]);
}
for(int i = 1; i < N; i++) {
int j = i;
while(j > 0 && a[j-1] > a[j]) {
swap(&a[j-1], &a[j], &j);
//int temp = a[j];
//a[j] = a[j-1]; …Run Code Online (Sandbox Code Playgroud) 实际上我使用UserId与客户端范围.
我可以直接在组件文件中使用Client范围,还是仅通过参数范围使用?鉴于以下代码哪一个是正确的?或者两者都对吗?
使用客户端范围:
<cfset getUsers = Application.Users.getAllUsers()>
<cffunction name="getAllUsers" returntype="query">
<cfquery name="read" datasource="myDsn">
Select *
from Users
Where UserID = <cfqueryparam cfsqltype="cf_sql_integer" value="#Client.UserID#">
</cfquery>
<cfreturn read>
</cffunction>
Run Code Online (Sandbox Code Playgroud)
使用参数范围:
<cfset getUsers = Application.Users.getAllUsers(UserID = Client.UserID)>
<cffunction name="getAllUsers" returntype="query">
<cfargument name="UserID" type="any" required= "true">
<cfquery name="read" datasource="myDsn">
Select *
from Users
Where UserID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.UserID#">
</cfquery>
<cfreturn read>
</cffunction>
Run Code Online (Sandbox Code Playgroud)
请给我方便的解决方案.提前致谢.
我是编程语言的新手,并想知道是否可以将没有特定类型的参数传递给函数.例如,我有以下代码片段定义了一个add将占用内存块的函数,检查它是否通过另一个函数填充,然后将一个元素添加到与该内存块相关的列表中.
该元素可以是int,float或char.所以我想写:
add(arrs1,20); //or also
add(arrs2,'b'); //or also
add(arrs3, 4.5);
Run Code Online (Sandbox Code Playgroud)
其中arrs#定义为struct arrs arrs#,并且它们引用浮点数,整数或字符数组但不混合.我怎么能做到这一点?
int add(arrs list, NEW_ELEMENT){//adds NEW_ELEMENT at the end of an arrs
int check_if_resize;
check_if_resize=resize(list, list->size + 1);
list->ptr[list->used++] = NEW_ELEMENT;
return check_if_resize;
}
Run Code Online (Sandbox Code Playgroud)
我感谢您的帮助.
我有这样的功能friend_exists:
def friend_exists(request, pid):
result = False
try:
user = Friend.objects.get(pid=pid)
except Friend.DoesNotExist:
pass
if user:
result = True
return result
Run Code Online (Sandbox Code Playgroud)
我从我的其他函数调用它:
exists = friend_exists(form.cleaned_data['pid'])
Run Code Online (Sandbox Code Playgroud)
哪里pid = u'12345678'.为什么我得到:
Exception Type: TypeError at /user/register/
Exception Value: friend_exists() takes exactly 2 arguments (1 given)
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我试图通过main传递参数,这工作正常,然后检查传入的参数是否包含正确的格式/值.但是,即使我通过正确的格式,它仍然显示有错误,这里是代码:
int main(int argc, char* argv[]) {
/* Check if arguments are being passed through */
if(argc == 1){
cout << endl << "--- ERROR ---" << endl;
exit(0);
}
/* Check if the first argument contains the correct data */
string file_name = argv[1];
/* Handle operation */
string operation = argv[2];
if(operation != "-t" || operation != "-r")
{
cout << "Something is not right";
}
}
Run Code Online (Sandbox Code Playgroud)
如果我这样做:cout << operation;那么结果将是:-t当我运行应用程序时传递-t.
谁能建议我哪里出错了?
更新:
我会传递这些论点:
./main …