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?










share|improve this 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















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?










share|improve this 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













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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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










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 some_operation()'s argument x isn't defined, and you assign value.append() to value every time you iterate over int_arg.






share|improve this answer










New contributor




Clepsyd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















  • 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




















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 some_operation()'s argument x isn't defined, and you assign value.append() to value every time you iterate over int_arg.






share|improve this answer










New contributor




Clepsyd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















  • 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

















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 some_operation()'s argument x isn't defined, and you assign value.append() to value every time you iterate over int_arg.






share|improve this answer










New contributor




Clepsyd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















  • 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















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 some_operation()'s argument x isn't defined, and you assign value.append() to value every time you iterate over int_arg.






share|improve this answer










New contributor




Clepsyd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









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 some_operation()'s argument x isn't defined, and you assign value.append() to value every time you iterate over int_arg.







share|improve this answer










New contributor




Clepsyd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this answer



share|improve this answer








edited yesterday





















New contributor




Clepsyd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









answered yesterday









Clepsyd

196




196




New contributor




Clepsyd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Clepsyd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Clepsyd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • 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




















  • 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


















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





Popular posts from this blog

Quarter-circle Tiles

build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

Mont Emei