omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Problem with if statements checking True

    Pythonista
    4
    15
    7792
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • techteej
      techteej last edited by

      Correct me if I'm wrong, but shouldn't the following statement work based on if my variable is true or false?

      if twitter_mode:
                print "Hello"
      else:
                pass
      
      1 Reply Last reply Reply Quote 0
      • Webmaster4o
        Webmaster4o last edited by

        Try replacing twitter_mode with True for debugging purposes and see if it works.

        1 Reply Last reply Reply Quote 0
        • dgelessus
          dgelessus last edited by dgelessus

          Exactly. If the if condition is true, the first block is executed, otherwise the second (else) block. The condition doesn't need to be exactly True, it needs to be an object that is considered a true value. (To get the truth value of an object, use bool(obj). bool behaves exactly the same as an implicit conversion in an if statement condition.) Most objects are true, the most important cases of false objects are None, False, numbers that are exactly zero (like 0, 0.0, 0j, decimal.Decimal("0")), and containers that are empty (like (), [], {}, collections.OrderedDict(())).

          By the way, if your else block is empty, just remove it.

          If there is a specific piece of actual code that behaves differently than you'd expect, then please post that code. ;)

          1 Reply Last reply Reply Quote 2
          • techteej
            techteej last edited by techteej

            To address your points:

            • I meant to have my else block empty. I will fill later, (as this is an inner function)
            • I know how if statements work - but twitter_mode is a True or False variable and my code above isn't working.

            See the full WIP code here.

            1 Reply Last reply Reply Quote 0
            • Webmaster4o
              Webmaster4o last edited by

              Try removing twitter_mode from the if statement and replacing it with true or false. I think your problem exists because twitter_mode is being set incorrectly.

              1 Reply Last reply Reply Quote 0
              • techteej
                techteej last edited by

                @Webmaster4o Already had tried this in the initial debugging, but I also think there is an issue the way the variable is created- So I'm going to set up config.py and import that. I think it'll make things easier in the long run anyway.

                1 Reply Last reply Reply Quote 0
                • ccc
                  ccc last edited by

                  Watch out for the line twitter_mode = str(settings['twitter']) because bool(False) != bool('False').

                  1 Reply Last reply Reply Quote 0
                  • dgelessus
                    dgelessus last edited by

                    Try removing the local copies of config.twitter_mode and config.feed and always use the attributes of config instead. That way you won't have any issues with the local copy being different from the config attribute.

                    1 Reply Last reply Reply Quote 0
                    • techteej
                      techteej last edited by

                      @ccc Already took care of it ;) accidentally did this somehow..

                      @dgelessus Could you explain more in depth? Not sure what you mean.

                      1 Reply Last reply Reply Quote 0
                      • dgelessus
                        dgelessus last edited by

                        Near the beginning of your gist, you have these two lines:

                        news_url = config.feed
                        twitter_mode = config.twitter_mode
                        

                        What you're doing here is you're assigning the current values of config.feed and config.twitter_mode to the local (well, actually global, from the perspective of the current module) variables news_url and twitter_mode. Although the variables' values are the same, the variables themselves are not linked in any way. If you change config.twitter_mode, the global variable twitter_mode doesn't change, and vice versa.

                        Further on in your settings_action method, you declare news_url and twitter_mode as global, but never assign to them - instead you assign to config.feed and config.twitter_mode. Now the variables from config and the current module are no longer in sync. To avoid this issue, simply remove the globals news_url and twitter_mode, and always use config.feed and config.twitter_mode.

                        1 Reply Last reply Reply Quote 0
                        • techteej
                          techteej last edited by

                          @dgelessus Gotcha. Thanks for explaining that for me. It's been a long day...

                          1 Reply Last reply Reply Quote 0
                          • techteej
                            techteej last edited by

                            Would changing the variables be like writing to a file but writing over the current .py file?

                            1 Reply Last reply Reply Quote 0
                            • dgelessus
                              dgelessus last edited by

                              Not sure what you mean exactly. If you assign something to an attribute of the config module, you don't modify the actual source file. The changes are as temporary as any other variable. The important part is that you store your data in a single place (the config module) and not under multiple separate names.

                              1 Reply Last reply Reply Quote 0
                              • techteej
                                techteej last edited by techteej

                                @dgelessus Let me rephrase, How would I edit the variables in config.py? Would I edit it like any other text file?

                                1 Reply Last reply Reply Quote 0
                                • Webmaster4o
                                  Webmaster4o last edited by

                                  Yes, edit it like any other file.

                                  1 Reply Last reply Reply Quote 0
                                  • First post
                                    Last post
                                  Powered by NodeBB Forums | Contributors