How can a function optionally return one or more values









up vote
2
down vote

favorite












What is the preferred way to ignore an optional return values of function f()?



a)



foo, _ = f()


b)



foo = f()[0]


c)



def f(return_bar=True):
if return_bar:
return foo, bar
else:
return foo

foo = f(return_bar=False)









share|improve this question























  • definetly not C, A is more pythonic
    – Netwave
    May 18 '17 at 10:15







  • 2




    b) makes sense the most. c) is not scalable and a) just wastes space on a useless object.
    – Haris
    May 18 '17 at 10:15










  • a) is good if you want to skip value in the middle, like for path, _, files in os.walk(...)
    – volcano
    May 18 '17 at 10:18










  • @Haris what do you mean by scalable in this context?
    – Max
    May 18 '17 at 10:23










  • @Max, this cannot be a standard way of doing this because one cannot use this fro cases where a function can return 1, 2 or 3 return values.
    – Haris
    May 18 '17 at 10:25














up vote
2
down vote

favorite












What is the preferred way to ignore an optional return values of function f()?



a)



foo, _ = f()


b)



foo = f()[0]


c)



def f(return_bar=True):
if return_bar:
return foo, bar
else:
return foo

foo = f(return_bar=False)









share|improve this question























  • definetly not C, A is more pythonic
    – Netwave
    May 18 '17 at 10:15







  • 2




    b) makes sense the most. c) is not scalable and a) just wastes space on a useless object.
    – Haris
    May 18 '17 at 10:15










  • a) is good if you want to skip value in the middle, like for path, _, files in os.walk(...)
    – volcano
    May 18 '17 at 10:18










  • @Haris what do you mean by scalable in this context?
    – Max
    May 18 '17 at 10:23










  • @Max, this cannot be a standard way of doing this because one cannot use this fro cases where a function can return 1, 2 or 3 return values.
    – Haris
    May 18 '17 at 10:25












up vote
2
down vote

favorite









up vote
2
down vote

favorite











What is the preferred way to ignore an optional return values of function f()?



a)



foo, _ = f()


b)



foo = f()[0]


c)



def f(return_bar=True):
if return_bar:
return foo, bar
else:
return foo

foo = f(return_bar=False)









share|improve this question















What is the preferred way to ignore an optional return values of function f()?



a)



foo, _ = f()


b)



foo = f()[0]


c)



def f(return_bar=True):
if return_bar:
return foo, bar
else:
return foo

foo = f(return_bar=False)






python return






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 18:30









Amir

4,57042348




4,57042348










asked May 18 '17 at 10:13









Max

3314




3314











  • definetly not C, A is more pythonic
    – Netwave
    May 18 '17 at 10:15







  • 2




    b) makes sense the most. c) is not scalable and a) just wastes space on a useless object.
    – Haris
    May 18 '17 at 10:15










  • a) is good if you want to skip value in the middle, like for path, _, files in os.walk(...)
    – volcano
    May 18 '17 at 10:18










  • @Haris what do you mean by scalable in this context?
    – Max
    May 18 '17 at 10:23










  • @Max, this cannot be a standard way of doing this because one cannot use this fro cases where a function can return 1, 2 or 3 return values.
    – Haris
    May 18 '17 at 10:25
















  • definetly not C, A is more pythonic
    – Netwave
    May 18 '17 at 10:15







  • 2




    b) makes sense the most. c) is not scalable and a) just wastes space on a useless object.
    – Haris
    May 18 '17 at 10:15










  • a) is good if you want to skip value in the middle, like for path, _, files in os.walk(...)
    – volcano
    May 18 '17 at 10:18










  • @Haris what do you mean by scalable in this context?
    – Max
    May 18 '17 at 10:23










  • @Max, this cannot be a standard way of doing this because one cannot use this fro cases where a function can return 1, 2 or 3 return values.
    – Haris
    May 18 '17 at 10:25















definetly not C, A is more pythonic
– Netwave
May 18 '17 at 10:15





definetly not C, A is more pythonic
– Netwave
May 18 '17 at 10:15





2




2




b) makes sense the most. c) is not scalable and a) just wastes space on a useless object.
– Haris
May 18 '17 at 10:15




b) makes sense the most. c) is not scalable and a) just wastes space on a useless object.
– Haris
May 18 '17 at 10:15












a) is good if you want to skip value in the middle, like for path, _, files in os.walk(...)
– volcano
May 18 '17 at 10:18




a) is good if you want to skip value in the middle, like for path, _, files in os.walk(...)
– volcano
May 18 '17 at 10:18












@Haris what do you mean by scalable in this context?
– Max
May 18 '17 at 10:23




@Haris what do you mean by scalable in this context?
– Max
May 18 '17 at 10:23












@Max, this cannot be a standard way of doing this because one cannot use this fro cases where a function can return 1, 2 or 3 return values.
– Haris
May 18 '17 at 10:25




@Max, this cannot be a standard way of doing this because one cannot use this fro cases where a function can return 1, 2 or 3 return values.
– Haris
May 18 '17 at 10:25












1 Answer
1






active

oldest

votes

















up vote
3
down vote













You're setting yourself up for trouble if your function returns two variables sometimes and one variable another time.



foo, _ = f()


Usually using underscore to ignore variables is the standard practice, but in your case, if for whatever reason, this call to f() returned only one variable, you will get a runtime error.



Unless you can guarantee that f() will return two variables this time, it's better to do this



b = f()

if(isinstance(b, tuple)):
foo = b[0]





share|improve this answer


















  • 3




    "if this call to f() returned only one variable, you will get a runtime error." foo, *_ = f() solves this on Python 3. _ would simply be an empty list.
    – DeepSpace
    May 18 '17 at 10:20







  • 1




    where is the b coming from? is this a typo?
    – user463035818
    Aug 29 at 16:17










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44044932%2fhow-can-a-function-optionally-return-one-or-more-values%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote













You're setting yourself up for trouble if your function returns two variables sometimes and one variable another time.



foo, _ = f()


Usually using underscore to ignore variables is the standard practice, but in your case, if for whatever reason, this call to f() returned only one variable, you will get a runtime error.



Unless you can guarantee that f() will return two variables this time, it's better to do this



b = f()

if(isinstance(b, tuple)):
foo = b[0]





share|improve this answer


















  • 3




    "if this call to f() returned only one variable, you will get a runtime error." foo, *_ = f() solves this on Python 3. _ would simply be an empty list.
    – DeepSpace
    May 18 '17 at 10:20







  • 1




    where is the b coming from? is this a typo?
    – user463035818
    Aug 29 at 16:17














up vote
3
down vote













You're setting yourself up for trouble if your function returns two variables sometimes and one variable another time.



foo, _ = f()


Usually using underscore to ignore variables is the standard practice, but in your case, if for whatever reason, this call to f() returned only one variable, you will get a runtime error.



Unless you can guarantee that f() will return two variables this time, it's better to do this



b = f()

if(isinstance(b, tuple)):
foo = b[0]





share|improve this answer


















  • 3




    "if this call to f() returned only one variable, you will get a runtime error." foo, *_ = f() solves this on Python 3. _ would simply be an empty list.
    – DeepSpace
    May 18 '17 at 10:20







  • 1




    where is the b coming from? is this a typo?
    – user463035818
    Aug 29 at 16:17












up vote
3
down vote










up vote
3
down vote









You're setting yourself up for trouble if your function returns two variables sometimes and one variable another time.



foo, _ = f()


Usually using underscore to ignore variables is the standard practice, but in your case, if for whatever reason, this call to f() returned only one variable, you will get a runtime error.



Unless you can guarantee that f() will return two variables this time, it's better to do this



b = f()

if(isinstance(b, tuple)):
foo = b[0]





share|improve this answer














You're setting yourself up for trouble if your function returns two variables sometimes and one variable another time.



foo, _ = f()


Usually using underscore to ignore variables is the standard practice, but in your case, if for whatever reason, this call to f() returned only one variable, you will get a runtime error.



Unless you can guarantee that f() will return two variables this time, it's better to do this



b = f()

if(isinstance(b, tuple)):
foo = b[0]






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 9 at 18:50









Amir

4,57042348




4,57042348










answered May 18 '17 at 10:18









ハセン

27915




27915







  • 3




    "if this call to f() returned only one variable, you will get a runtime error." foo, *_ = f() solves this on Python 3. _ would simply be an empty list.
    – DeepSpace
    May 18 '17 at 10:20







  • 1




    where is the b coming from? is this a typo?
    – user463035818
    Aug 29 at 16:17












  • 3




    "if this call to f() returned only one variable, you will get a runtime error." foo, *_ = f() solves this on Python 3. _ would simply be an empty list.
    – DeepSpace
    May 18 '17 at 10:20







  • 1




    where is the b coming from? is this a typo?
    – user463035818
    Aug 29 at 16:17







3




3




"if this call to f() returned only one variable, you will get a runtime error." foo, *_ = f() solves this on Python 3. _ would simply be an empty list.
– DeepSpace
May 18 '17 at 10:20





"if this call to f() returned only one variable, you will get a runtime error." foo, *_ = f() solves this on Python 3. _ would simply be an empty list.
– DeepSpace
May 18 '17 at 10:20





1




1




where is the b coming from? is this a typo?
– user463035818
Aug 29 at 16:17




where is the b coming from? is this a typo?
– user463035818
Aug 29 at 16:17

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44044932%2fhow-can-a-function-optionally-return-one-or-more-values%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20