Python default values and setting the value based on other variables. To if else or to not.












3















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.










share|improve this question

























  • 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
















3















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.










share|improve this question

























  • 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














3












3








3








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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



















  • 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












3 Answers
3






active

oldest

votes


















6














How about this:



y = '%s-other' % x if x else 'some_default'





share|improve this answer































    3














    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




    1. Use setdefault like x.setdefault(some_key,some_default)=other

    2. 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.






    share|improve this answer
























    • +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





      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



















    0














    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 :)






    share|improve this answer























      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
      });


      }
      });














      draft saved

      draft discarded


















      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









      6














      How about this:



      y = '%s-other' % x if x else 'some_default'





      share|improve this answer




























        6














        How about this:



        y = '%s-other' % x if x else 'some_default'





        share|improve this answer


























          6












          6








          6







          How about this:



          y = '%s-other' % x if x else 'some_default'





          share|improve this answer













          How about this:



          y = '%s-other' % x if x else 'some_default'






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 4 '12 at 19:36









          Rob WoutersRob Wouters

          11.4k22732




          11.4k22732

























              3














              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




              1. Use setdefault like x.setdefault(some_key,some_default)=other

              2. 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.






              share|improve this answer
























              • +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





                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
















              3














              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




              1. Use setdefault like x.setdefault(some_key,some_default)=other

              2. 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.






              share|improve this answer
























              • +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





                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














              3












              3








              3







              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




              1. Use setdefault like x.setdefault(some_key,some_default)=other

              2. 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.






              share|improve this answer













              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




              1. Use setdefault like x.setdefault(some_key,some_default)=other

              2. 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.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jan 4 '12 at 19:53









              AbhijitAbhijit

              44.7k1075164




              44.7k1075164













              • +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





                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 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





                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 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











              0














              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 :)






              share|improve this answer




























                0














                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 :)






                share|improve this answer


























                  0












                  0








                  0







                  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 :)






                  share|improve this answer













                  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 :)







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 4 '12 at 20:12









                  Roman BodnarchukRoman Bodnarchuk

                  20.9k64970




                  20.9k64970






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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

                      Costa Masnaga

                      Fotorealismo

                      Sidney Franklin