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.


    Inheritance and **kwargs popping/consumption

    Pythonista
    4
    9
    4073
    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.
    • Phuket2
      Phuket2 last edited by

      class TVView(ui.View):
      	def __init__(self, data, *args, **kwargs):
      		ui.View.__init__(self, *args, **kwargs)
      

      Is there some trick to find out what kwargs have been consumed by the call to ui.View init

      • list itemin the above example. I have seen in classes where the kwarg is used, it's pop'ed. I have tested this with ui.View, as far as I can understand the kwargs are not consumed/popped. Not saying they should be. But I would have thought this would be a common problem when using 3rd party Libs. But i can't find any specific information on it.
      1 Reply Last reply Reply Quote 0
      • omz
        omz last edited by omz

        There's no way to find out which of the keyword arguments a method actually uses. Why would you want that?

        Phuket2 1 Reply Last reply Reply Quote 0
        • Phuket2
          Phuket2 @omz last edited by

          @omz , just to be able to act with certainty. Eg, as far as I know, you don't handle all kwargs passed to ui Elements. So by me calling the ui.View init method is totally redundant as i would have to iterate through all the kwargs again setting the attrs if I don't know what has been already handled. At least that is my understanding.

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

            See the first code block under http://omz-software.com/pythonista/docs/ios/ui.html#building-custom-views

            You don't need to call ui.View.__init__().

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

              @ccc thanks. I see the comments about no need to call the super now that you pointed out. But I have being doing monkey see monkey do. I have seen code from omz where he passes on the args and kwargs to the super. Maybe it's old code. But I am sure I tested the above, and I don't think it gets passed on without calling init or super. I will check again.

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

                # coding: utf-8
                import ui
                
                class doesnotcallsuperinit(ui.View):
                   def __init__(self,*args,**kwargs):
                      pass
                
                class hasnoinit(ui.View):
                   def somotherfunc(self):
                      pass
                      
                class callssuperinit(ui.View):
                   def __init__(self,*args,**kwargs):
                      ui.View.__init__(self,*args,**kwargs)
                def test_class(cls):
                   obj=cls(bg_color='red')
                   print '{} kwargs were {} properly initted'.format(cls.__name__,
                               'NOT'*(not obj.bg_color==(1,0,0,1)))
                
                test_class(doesnotcallsuperinit)
                test_class(hasnoinit)
                test_class(callssuperinit)
                '''
                doesnotcallsuperinit kwargs were NOT properly initted
                hasnoinit kwargs were  properly initted
                callssuperinit kwargs were  properly initted
                '''
                

                No need to call ui.View.init if you don't provide your own. If you do, you must call it.

                In your case, you are really just better off handling them yourself, since not all attributes are settable in the constructor, and different components don't behave the way they are supposed to ( for example, TableView did not respond to flex in the constructor, at least in the last beta, Buttons and Labels i think behave differently if setting the title in the constructor vs after, i.e it autoresizes or not). Write a function that sets attributes then forget about it.

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

                  @JonB , thanks. It's very clear how you have explained it. I was trying to be correct. But respectively to @omz, the bottom line is that when he gets a chance to catch up, all args, kwargs should be handled in the constructors. When it's not , causes you too write a lot of extra code. It's not that it's that difficult, it just gets messy. If you could just pass a dict/kwargs to any ui element and have the attrs set, would be so nice with one addition, the parent view. If the parent is passed, then the constructor could add the new element to the sub view of the parent. Then you get a one liner.

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

                    For example, it would be nice if Scene.ShapeNode.__init__() took an anchor_point parameter.

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

                      Three lines (or one if you want to use list comprehension) isn't that much extra code. I agree it would be nice. If you want, you can make your own base class from which all of your customs inherit. Then simply import your custom class along with ui.

                      #. in customview.py
                      import ui
                      class CustomView(ui.View):
                         def __init__(self,*args,**kwargs):
                            ui.View.__init__(self,*args,**kwargs)
                            for key in kwargs:
                               if hasattr(self,key):
                                  setattr(self,key,kwargs[key])
                      
                      # in someotherview.py
                      import ui
                      from customview import CustomView
                      class MyView(CustomView):
                         def __init__(self,args,kwargs):
                            CustomView.__init__(self,args,kwargs)
                            #rest of your custom code
                      
                      1 Reply Last reply Reply Quote 1
                      • First post
                        Last post
                      Powered by NodeBB Forums | Contributors