How to write function that can handle input argument of either a single value or multiple values (i.e. in a...
up vote
-5
down vote
favorite
I have some code that takes an integer as an input argument, and I want it to be able to handle a list of integers as well. I am trying to decide the best method for handling such inputs. A simplified version of my function would be as follows:
def compute_value(int_arg):
value =
for x in int_arg:
value = value.append(some_operation(x))
return value
If I use this for
loop, an error will occur because an integer isn't iterable. To solve this, it seems to require converting int_arg
to a list at the start of the code:
def compute_value(int_arg):
int_arg = [int_arg] if type(int_arg)==int else int_arg
...
This kind of check seems a bit cumbersome, especially when writing many functions that behave the same way (i.e. operate on either a single value or multiple values). Is there a better way to write this type of function so that it can handle things robustly?
python validation
put on hold as off-topic by 200_success, Sᴀᴍ Onᴇᴌᴀ, Mast, Jamal♦ 22 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – 200_success, Sᴀᴍ Onᴇᴌᴀ, Mast, Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
-5
down vote
favorite
I have some code that takes an integer as an input argument, and I want it to be able to handle a list of integers as well. I am trying to decide the best method for handling such inputs. A simplified version of my function would be as follows:
def compute_value(int_arg):
value =
for x in int_arg:
value = value.append(some_operation(x))
return value
If I use this for
loop, an error will occur because an integer isn't iterable. To solve this, it seems to require converting int_arg
to a list at the start of the code:
def compute_value(int_arg):
int_arg = [int_arg] if type(int_arg)==int else int_arg
...
This kind of check seems a bit cumbersome, especially when writing many functions that behave the same way (i.e. operate on either a single value or multiple values). Is there a better way to write this type of function so that it can handle things robustly?
python validation
put on hold as off-topic by 200_success, Sᴀᴍ Onᴇᴌᴀ, Mast, Jamal♦ 22 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – 200_success, Sᴀᴍ Onᴇᴌᴀ, Mast, Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.
1
Generic foo/bar examples are off-topic for Code Review. Please present a concrete implementation for review so that we can advise you properly. See the help center and How to Ask.
– 200_success
yesterday
Thanks. Where would be a better forum for asking generic code-style/best practices questions?
– teepee
yesterday
You could try Stack Overflow, but I think that you would get better advice if you just posted your real code here.
– 200_success
yesterday
add a comment |
up vote
-5
down vote
favorite
up vote
-5
down vote
favorite
I have some code that takes an integer as an input argument, and I want it to be able to handle a list of integers as well. I am trying to decide the best method for handling such inputs. A simplified version of my function would be as follows:
def compute_value(int_arg):
value =
for x in int_arg:
value = value.append(some_operation(x))
return value
If I use this for
loop, an error will occur because an integer isn't iterable. To solve this, it seems to require converting int_arg
to a list at the start of the code:
def compute_value(int_arg):
int_arg = [int_arg] if type(int_arg)==int else int_arg
...
This kind of check seems a bit cumbersome, especially when writing many functions that behave the same way (i.e. operate on either a single value or multiple values). Is there a better way to write this type of function so that it can handle things robustly?
python validation
I have some code that takes an integer as an input argument, and I want it to be able to handle a list of integers as well. I am trying to decide the best method for handling such inputs. A simplified version of my function would be as follows:
def compute_value(int_arg):
value =
for x in int_arg:
value = value.append(some_operation(x))
return value
If I use this for
loop, an error will occur because an integer isn't iterable. To solve this, it seems to require converting int_arg
to a list at the start of the code:
def compute_value(int_arg):
int_arg = [int_arg] if type(int_arg)==int else int_arg
...
This kind of check seems a bit cumbersome, especially when writing many functions that behave the same way (i.e. operate on either a single value or multiple values). Is there a better way to write this type of function so that it can handle things robustly?
python validation
python validation
edited yesterday
200_success
127k15149412
127k15149412
asked yesterday
teepee
1535
1535
put on hold as off-topic by 200_success, Sᴀᴍ Onᴇᴌᴀ, Mast, Jamal♦ 22 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – 200_success, Sᴀᴍ Onᴇᴌᴀ, Mast, Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as off-topic by 200_success, Sᴀᴍ Onᴇᴌᴀ, Mast, Jamal♦ 22 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – 200_success, Sᴀᴍ Onᴇᴌᴀ, Mast, Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.
1
Generic foo/bar examples are off-topic for Code Review. Please present a concrete implementation for review so that we can advise you properly. See the help center and How to Ask.
– 200_success
yesterday
Thanks. Where would be a better forum for asking generic code-style/best practices questions?
– teepee
yesterday
You could try Stack Overflow, but I think that you would get better advice if you just posted your real code here.
– 200_success
yesterday
add a comment |
1
Generic foo/bar examples are off-topic for Code Review. Please present a concrete implementation for review so that we can advise you properly. See the help center and How to Ask.
– 200_success
yesterday
Thanks. Where would be a better forum for asking generic code-style/best practices questions?
– teepee
yesterday
You could try Stack Overflow, but I think that you would get better advice if you just posted your real code here.
– 200_success
yesterday
1
1
Generic foo/bar examples are off-topic for Code Review. Please present a concrete implementation for review so that we can advise you properly. See the help center and How to Ask.
– 200_success
yesterday
Generic foo/bar examples are off-topic for Code Review. Please present a concrete implementation for review so that we can advise you properly. See the help center and How to Ask.
– 200_success
yesterday
Thanks. Where would be a better forum for asking generic code-style/best practices questions?
– teepee
yesterday
Thanks. Where would be a better forum for asking generic code-style/best practices questions?
– teepee
yesterday
You could try Stack Overflow, but I think that you would get better advice if you just posted your real code here.
– 200_success
yesterday
You could try Stack Overflow, but I think that you would get better advice if you just posted your real code here.
– 200_success
yesterday
add a comment |
1 Answer
1
active
oldest
votes
up vote
-2
down vote
Could you use try/except to handle the error?
def compute_value(int_arg):
value =
try:
for x in int_arg:
value = value.append(some_operation(x))
return value
except TypeError:
return int_arg
Also I noticed (edit:)your function , and you assign some_operation()
's argument x
isn't definedvalue.append()
to value
every time you iterate over int_arg
.
New contributor
I fixed the assignment error, thanks. Also, in your code, should theexcept
part havevalue = some_operation(x); return value
? (Also, I didn't includesome_operation(x)
because it could be many different things in my case.
– teepee
yesterday
Sorry my bad, x is defined in the for loop... misread the code. And then yes, or justreturn some_operation(int_arg)
.
– Clepsyd
yesterday
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
-2
down vote
Could you use try/except to handle the error?
def compute_value(int_arg):
value =
try:
for x in int_arg:
value = value.append(some_operation(x))
return value
except TypeError:
return int_arg
Also I noticed (edit:)your function , and you assign some_operation()
's argument x
isn't definedvalue.append()
to value
every time you iterate over int_arg
.
New contributor
I fixed the assignment error, thanks. Also, in your code, should theexcept
part havevalue = some_operation(x); return value
? (Also, I didn't includesome_operation(x)
because it could be many different things in my case.
– teepee
yesterday
Sorry my bad, x is defined in the for loop... misread the code. And then yes, or justreturn some_operation(int_arg)
.
– Clepsyd
yesterday
add a comment |
up vote
-2
down vote
Could you use try/except to handle the error?
def compute_value(int_arg):
value =
try:
for x in int_arg:
value = value.append(some_operation(x))
return value
except TypeError:
return int_arg
Also I noticed (edit:)your function , and you assign some_operation()
's argument x
isn't definedvalue.append()
to value
every time you iterate over int_arg
.
New contributor
I fixed the assignment error, thanks. Also, in your code, should theexcept
part havevalue = some_operation(x); return value
? (Also, I didn't includesome_operation(x)
because it could be many different things in my case.
– teepee
yesterday
Sorry my bad, x is defined in the for loop... misread the code. And then yes, or justreturn some_operation(int_arg)
.
– Clepsyd
yesterday
add a comment |
up vote
-2
down vote
up vote
-2
down vote
Could you use try/except to handle the error?
def compute_value(int_arg):
value =
try:
for x in int_arg:
value = value.append(some_operation(x))
return value
except TypeError:
return int_arg
Also I noticed (edit:)your function , and you assign some_operation()
's argument x
isn't definedvalue.append()
to value
every time you iterate over int_arg
.
New contributor
Could you use try/except to handle the error?
def compute_value(int_arg):
value =
try:
for x in int_arg:
value = value.append(some_operation(x))
return value
except TypeError:
return int_arg
Also I noticed (edit:)your function , and you assign some_operation()
's argument x
isn't definedvalue.append()
to value
every time you iterate over int_arg
.
New contributor
edited yesterday
New contributor
answered yesterday
Clepsyd
196
196
New contributor
New contributor
I fixed the assignment error, thanks. Also, in your code, should theexcept
part havevalue = some_operation(x); return value
? (Also, I didn't includesome_operation(x)
because it could be many different things in my case.
– teepee
yesterday
Sorry my bad, x is defined in the for loop... misread the code. And then yes, or justreturn some_operation(int_arg)
.
– Clepsyd
yesterday
add a comment |
I fixed the assignment error, thanks. Also, in your code, should theexcept
part havevalue = some_operation(x); return value
? (Also, I didn't includesome_operation(x)
because it could be many different things in my case.
– teepee
yesterday
Sorry my bad, x is defined in the for loop... misread the code. And then yes, or justreturn some_operation(int_arg)
.
– Clepsyd
yesterday
I fixed the assignment error, thanks. Also, in your code, should the
except
part have value = some_operation(x); return value
? (Also, I didn't include some_operation(x)
because it could be many different things in my case.– teepee
yesterday
I fixed the assignment error, thanks. Also, in your code, should the
except
part have value = some_operation(x); return value
? (Also, I didn't include some_operation(x)
because it could be many different things in my case.– teepee
yesterday
Sorry my bad, x is defined in the for loop... misread the code. And then yes, or just
return some_operation(int_arg)
.– Clepsyd
yesterday
Sorry my bad, x is defined in the for loop... misread the code. And then yes, or just
return some_operation(int_arg)
.– Clepsyd
yesterday
add a comment |
1
Generic foo/bar examples are off-topic for Code Review. Please present a concrete implementation for review so that we can advise you properly. See the help center and How to Ask.
– 200_success
yesterday
Thanks. Where would be a better forum for asking generic code-style/best practices questions?
– teepee
yesterday
You could try Stack Overflow, but I think that you would get better advice if you just posted your real code here.
– 200_success
yesterday