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)
python return
|
show 2 more comments
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)
python return
definetly notC
,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
|
show 2 more comments
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)
python return
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
python return
edited Nov 9 at 18:30
Amir
4,57042348
4,57042348
asked May 18 '17 at 10:13
Max
3314
3314
definetly notC
,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
|
show 2 more comments
definetly notC
,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
|
show 2 more comments
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]
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 theb
coming from? is this a typo?
– user463035818
Aug 29 at 16:17
add a comment |
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]
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 theb
coming from? is this a typo?
– user463035818
Aug 29 at 16:17
add a comment |
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]
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 theb
coming from? is this a typo?
– user463035818
Aug 29 at 16:17
add a comment |
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]
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]
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 theb
coming from? is this a typo?
– user463035818
Aug 29 at 16:17
add a comment |
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 theb
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
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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