Python default values and setting the value based on other variables. To if else or to not.
At times I have a variable that I want to default to something and if something else is set change it to something else.
The question is. What is preferred? Setting the default value and then changing it if the condition is met or setting the condition only once depending on the initial check with an added else?
Example in code.
if x:
y = '%s-other' % x
else:
y = 'some_default'
The other option would be.
y = 'some_default'
if x:
y = '%s-other' % x
This isn't always an argument passed in to a function so kwargs is not something I want to rely on here.
Personally, the second one seems to be a lot more clear to me but what I have yet to find any sort of opinion by anyone on this.
python coding-style syntax default-value
add a comment |
At times I have a variable that I want to default to something and if something else is set change it to something else.
The question is. What is preferred? Setting the default value and then changing it if the condition is met or setting the condition only once depending on the initial check with an added else?
Example in code.
if x:
y = '%s-other' % x
else:
y = 'some_default'
The other option would be.
y = 'some_default'
if x:
y = '%s-other' % x
This isn't always an argument passed in to a function so kwargs is not something I want to rely on here.
Personally, the second one seems to be a lot more clear to me but what I have yet to find any sort of opinion by anyone on this.
python coding-style syntax default-value
Note you can format lines as code by indenting them four spaces. The "{}" button in the editor toolbar does this for you. Edit your question and try it out. Click the orange question mark in the editor toolbar for more information and tips on formatting.
– outis
Jan 4 '12 at 19:38
1
Near identical dupe of stackoverflow.com/questions/8730946/…
– tkone
Jan 4 '12 at 19:45
It is a near identical dupe but different, I asked both of them :) I could have consolidated it in to one. Not sure if I can do that now?
– ScottZ
Jan 4 '12 at 20:18
add a comment |
At times I have a variable that I want to default to something and if something else is set change it to something else.
The question is. What is preferred? Setting the default value and then changing it if the condition is met or setting the condition only once depending on the initial check with an added else?
Example in code.
if x:
y = '%s-other' % x
else:
y = 'some_default'
The other option would be.
y = 'some_default'
if x:
y = '%s-other' % x
This isn't always an argument passed in to a function so kwargs is not something I want to rely on here.
Personally, the second one seems to be a lot more clear to me but what I have yet to find any sort of opinion by anyone on this.
python coding-style syntax default-value
At times I have a variable that I want to default to something and if something else is set change it to something else.
The question is. What is preferred? Setting the default value and then changing it if the condition is met or setting the condition only once depending on the initial check with an added else?
Example in code.
if x:
y = '%s-other' % x
else:
y = 'some_default'
The other option would be.
y = 'some_default'
if x:
y = '%s-other' % x
This isn't always an argument passed in to a function so kwargs is not something I want to rely on here.
Personally, the second one seems to be a lot more clear to me but what I have yet to find any sort of opinion by anyone on this.
python coding-style syntax default-value
python coding-style syntax default-value
edited Jan 4 '12 at 19:42
tback
6,71142956
6,71142956
asked Jan 4 '12 at 19:34
ScottZScottZ
22729
22729
Note you can format lines as code by indenting them four spaces. The "{}" button in the editor toolbar does this for you. Edit your question and try it out. Click the orange question mark in the editor toolbar for more information and tips on formatting.
– outis
Jan 4 '12 at 19:38
1
Near identical dupe of stackoverflow.com/questions/8730946/…
– tkone
Jan 4 '12 at 19:45
It is a near identical dupe but different, I asked both of them :) I could have consolidated it in to one. Not sure if I can do that now?
– ScottZ
Jan 4 '12 at 20:18
add a comment |
Note you can format lines as code by indenting them four spaces. The "{}" button in the editor toolbar does this for you. Edit your question and try it out. Click the orange question mark in the editor toolbar for more information and tips on formatting.
– outis
Jan 4 '12 at 19:38
1
Near identical dupe of stackoverflow.com/questions/8730946/…
– tkone
Jan 4 '12 at 19:45
It is a near identical dupe but different, I asked both of them :) I could have consolidated it in to one. Not sure if I can do that now?
– ScottZ
Jan 4 '12 at 20:18
Note you can format lines as code by indenting them four spaces. The "{}" button in the editor toolbar does this for you. Edit your question and try it out. Click the orange question mark in the editor toolbar for more information and tips on formatting.
– outis
Jan 4 '12 at 19:38
Note you can format lines as code by indenting them four spaces. The "{}" button in the editor toolbar does this for you. Edit your question and try it out. Click the orange question mark in the editor toolbar for more information and tips on formatting.
– outis
Jan 4 '12 at 19:38
1
1
Near identical dupe of stackoverflow.com/questions/8730946/…
– tkone
Jan 4 '12 at 19:45
Near identical dupe of stackoverflow.com/questions/8730946/…
– tkone
Jan 4 '12 at 19:45
It is a near identical dupe but different, I asked both of them :) I could have consolidated it in to one. Not sure if I can do that now?
– ScottZ
Jan 4 '12 at 20:18
It is a near identical dupe but different, I asked both of them :) I could have consolidated it in to one. Not sure if I can do that now?
– ScottZ
Jan 4 '12 at 20:18
add a comment |
3 Answers
3
active
oldest
votes
How about this:
y = '%s-other' % x if x else 'some_default'
add a comment |
As Rob pointed out,
y = '%s-other' % x if x else 'some_default'
is a very common construct across various language
Python provides some more alternatives and the choice depends on the User
y = ['some_default','%s-other'][x!=None]
If you are dealing with dictionary, it already has two options
- Use setdefault like
x.setdefault(some_key,some_default)=other
- Use collections.defaultdict
The other's you posted are also valid but not very pythonic but yet you will encounter lot of code with your quoted style.
To me, as long as a program is readable, efficient, we should not be too bogged down to make some contruct pythonic which often deviates the focus.
+1 for the alternatives, althoughbool(x)
may be better thanx!=None
in the array indexing option.
– Andrew Clark
Jan 4 '12 at 20:05
2
1. Rob's variant is better than[if_false, if_true][cond]
. If you still use it thenNone
is a singleton in Python so it should bex is not None
instead ofx != None
. 2.y = x.setdefault(some_key, 'some_default')
, otherwise itSyntaxError: can't assign to function call
.
– jfs
Jan 5 '12 at 0:32
add a comment |
More sugar to other answers:
y = x and '%s-other' % x or 'some_default'
But this one may scare people so I'd recomend to use Rob's one :)
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f8732867%2fpython-default-values-and-setting-the-value-based-on-other-variables-to-if-else%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
How about this:
y = '%s-other' % x if x else 'some_default'
add a comment |
How about this:
y = '%s-other' % x if x else 'some_default'
add a comment |
How about this:
y = '%s-other' % x if x else 'some_default'
How about this:
y = '%s-other' % x if x else 'some_default'
answered Jan 4 '12 at 19:36
Rob WoutersRob Wouters
11.4k22732
11.4k22732
add a comment |
add a comment |
As Rob pointed out,
y = '%s-other' % x if x else 'some_default'
is a very common construct across various language
Python provides some more alternatives and the choice depends on the User
y = ['some_default','%s-other'][x!=None]
If you are dealing with dictionary, it already has two options
- Use setdefault like
x.setdefault(some_key,some_default)=other
- Use collections.defaultdict
The other's you posted are also valid but not very pythonic but yet you will encounter lot of code with your quoted style.
To me, as long as a program is readable, efficient, we should not be too bogged down to make some contruct pythonic which often deviates the focus.
+1 for the alternatives, althoughbool(x)
may be better thanx!=None
in the array indexing option.
– Andrew Clark
Jan 4 '12 at 20:05
2
1. Rob's variant is better than[if_false, if_true][cond]
. If you still use it thenNone
is a singleton in Python so it should bex is not None
instead ofx != None
. 2.y = x.setdefault(some_key, 'some_default')
, otherwise itSyntaxError: can't assign to function call
.
– jfs
Jan 5 '12 at 0:32
add a comment |
As Rob pointed out,
y = '%s-other' % x if x else 'some_default'
is a very common construct across various language
Python provides some more alternatives and the choice depends on the User
y = ['some_default','%s-other'][x!=None]
If you are dealing with dictionary, it already has two options
- Use setdefault like
x.setdefault(some_key,some_default)=other
- Use collections.defaultdict
The other's you posted are also valid but not very pythonic but yet you will encounter lot of code with your quoted style.
To me, as long as a program is readable, efficient, we should not be too bogged down to make some contruct pythonic which often deviates the focus.
+1 for the alternatives, althoughbool(x)
may be better thanx!=None
in the array indexing option.
– Andrew Clark
Jan 4 '12 at 20:05
2
1. Rob's variant is better than[if_false, if_true][cond]
. If you still use it thenNone
is a singleton in Python so it should bex is not None
instead ofx != None
. 2.y = x.setdefault(some_key, 'some_default')
, otherwise itSyntaxError: can't assign to function call
.
– jfs
Jan 5 '12 at 0:32
add a comment |
As Rob pointed out,
y = '%s-other' % x if x else 'some_default'
is a very common construct across various language
Python provides some more alternatives and the choice depends on the User
y = ['some_default','%s-other'][x!=None]
If you are dealing with dictionary, it already has two options
- Use setdefault like
x.setdefault(some_key,some_default)=other
- Use collections.defaultdict
The other's you posted are also valid but not very pythonic but yet you will encounter lot of code with your quoted style.
To me, as long as a program is readable, efficient, we should not be too bogged down to make some contruct pythonic which often deviates the focus.
As Rob pointed out,
y = '%s-other' % x if x else 'some_default'
is a very common construct across various language
Python provides some more alternatives and the choice depends on the User
y = ['some_default','%s-other'][x!=None]
If you are dealing with dictionary, it already has two options
- Use setdefault like
x.setdefault(some_key,some_default)=other
- Use collections.defaultdict
The other's you posted are also valid but not very pythonic but yet you will encounter lot of code with your quoted style.
To me, as long as a program is readable, efficient, we should not be too bogged down to make some contruct pythonic which often deviates the focus.
answered Jan 4 '12 at 19:53
AbhijitAbhijit
44.7k1075164
44.7k1075164
+1 for the alternatives, althoughbool(x)
may be better thanx!=None
in the array indexing option.
– Andrew Clark
Jan 4 '12 at 20:05
2
1. Rob's variant is better than[if_false, if_true][cond]
. If you still use it thenNone
is a singleton in Python so it should bex is not None
instead ofx != None
. 2.y = x.setdefault(some_key, 'some_default')
, otherwise itSyntaxError: can't assign to function call
.
– jfs
Jan 5 '12 at 0:32
add a comment |
+1 for the alternatives, althoughbool(x)
may be better thanx!=None
in the array indexing option.
– Andrew Clark
Jan 4 '12 at 20:05
2
1. Rob's variant is better than[if_false, if_true][cond]
. If you still use it thenNone
is a singleton in Python so it should bex is not None
instead ofx != None
. 2.y = x.setdefault(some_key, 'some_default')
, otherwise itSyntaxError: can't assign to function call
.
– jfs
Jan 5 '12 at 0:32
+1 for the alternatives, although
bool(x)
may be better than x!=None
in the array indexing option.– Andrew Clark
Jan 4 '12 at 20:05
+1 for the alternatives, although
bool(x)
may be better than x!=None
in the array indexing option.– Andrew Clark
Jan 4 '12 at 20:05
2
2
1. Rob's variant is better than
[if_false, if_true][cond]
. If you still use it then None
is a singleton in Python so it should be x is not None
instead of x != None
. 2. y = x.setdefault(some_key, 'some_default')
, otherwise it SyntaxError: can't assign to function call
.– jfs
Jan 5 '12 at 0:32
1. Rob's variant is better than
[if_false, if_true][cond]
. If you still use it then None
is a singleton in Python so it should be x is not None
instead of x != None
. 2. y = x.setdefault(some_key, 'some_default')
, otherwise it SyntaxError: can't assign to function call
.– jfs
Jan 5 '12 at 0:32
add a comment |
More sugar to other answers:
y = x and '%s-other' % x or 'some_default'
But this one may scare people so I'd recomend to use Rob's one :)
add a comment |
More sugar to other answers:
y = x and '%s-other' % x or 'some_default'
But this one may scare people so I'd recomend to use Rob's one :)
add a comment |
More sugar to other answers:
y = x and '%s-other' % x or 'some_default'
But this one may scare people so I'd recomend to use Rob's one :)
More sugar to other answers:
y = x and '%s-other' % x or 'some_default'
But this one may scare people so I'd recomend to use Rob's one :)
answered Jan 4 '12 at 20:12
Roman BodnarchukRoman Bodnarchuk
20.9k64970
20.9k64970
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f8732867%2fpython-default-values-and-setting-the-value-based-on-other-variables-to-if-else%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
Note you can format lines as code by indenting them four spaces. The "{}" button in the editor toolbar does this for you. Edit your question and try it out. Click the orange question mark in the editor toolbar for more information and tips on formatting.
– outis
Jan 4 '12 at 19:38
1
Near identical dupe of stackoverflow.com/questions/8730946/…
– tkone
Jan 4 '12 at 19:45
It is a near identical dupe but different, I asked both of them :) I could have consolidated it in to one. Not sure if I can do that now?
– ScottZ
Jan 4 '12 at 20:18