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.


    How to call 'self.__' outside of 'self'

    Pythonista
    game scene
    4
    15
    7939
    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 techteej

      Is there any way to call a 'self.__' outside of the scene that it is originally in? I'm trying to add a menu where you choose a color, then you can play as that color. Player color is defined in scene, but the menu is outside of the scene.

      Edit: Here is a gist with .py and .pyui files

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

        You could add a get_player_color() method to your scene:

        class MyScene(scene.Scene):
            ...
            def get_player_color(self):
                return self.player_color
            ...
        the_scene = MyScene()
        ...
        print(the_scene.get_player_color())
        
        
        1 Reply Last reply Reply Quote 0
        • techteej
          techteej last edited by

          This gives me the error that the player_color isn't there, but it is. :[

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

            Did you assign self.player_color inside __init__? If not, it won't exist!

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

              The problem here is that you're naming attributes/methods starting with two underscores. If the name doesn't also end with two underscores (e. g. __init__), the name is "mangled" internally into the form _<CLASSNAME>__<ATTRNAME>. This name will of course only resolve correctly inside the same class, in other classes or on module level it will refer to a nonexistant name. This functionality is mainly there to avoid naming conflicts with potential subclasses. In 99.9% of all cases this is not necessary, and it is best to not use it at all.

              If you're trying to make an attribute or method "private" as you would in Java, that's practically impossible in Python (or Java, cough reflection cough) because there are no access modifiers.

              TL;DR: Don't start non-special names with two underscores.

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

                This post is deleted!
                1 Reply Last reply Reply Quote 0
                • dgelessus
                  dgelessus last edited by

                  The .pyui file doesn't seem to be included in the gist.

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

                    This post is deleted!
                    1 Reply Last reply Reply Quote 0
                    • JonB
                      JonB last edited by

                      Looks like your problem is that you update the color in an instance on HockeyScene ................
                      But what you actually run is a new instance without the modified colors. See comment on gist.

                      The other problem is that your players are only defined within setup, which doesn't run until you run the scene. You should define players within __init__ if you want those attributes to exist. See. Y comment three comments ago!

                      I think it would make more sense to have your scene class take as arguments in __init__, the colors, or maybe player objects, rather than trying to modify an existing HockeyScene object from your color picker ui.

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

                        Could you show how I could have the scene class take as arguments in __init__?

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

                          When I try to do it, no colors show up at all, and the scene loads quite a bit longer.

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

                            Some suggestions add to gist comments.

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

                              This gives me the error:

                              AttributeError: 'tuple' object has no attribute 'title'

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

                                That is because a player_color_name is a string like 'Red' or 'Gold'. If you pass in a string with one of the color names in color_dict that will work better then passing in a tuple.

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

                                  This post is deleted!
                                  1 Reply Last reply Reply Quote 0
                                  • First post
                                    Last post
                                  Powered by NodeBB Forums | Contributors