Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. initializing an attribute in __init__
QtWS25 Last Chance

initializing an attribute in __init__

Scheduled Pinned Locked Moved Solved Language Bindings
10 Posts 3 Posters 3.4k Views
  • 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.
  • U Offline
    U Offline
    user4592357
    wrote on last edited by
    #1

    creating a subclass of Qt class which has custom attributes.

    Python complains when an attribute isn't constructed in __init__ and is constructed in another method, you get something like this:
    AttributeError: 'Window' object has no attribute '_Window__menubar'

    so what i do is:

    # in __init__()
    self.__menubar = self.menuBar()
    self.__create_menubar()
    
    # in __create_menubar()
    menu = self.__menubar.addMenu('menu')
    ...
    

    this code doesn't feel "comfortable" because i init the menu bar (or a widget), then add properties to it in another method.

    is this the only way?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What about using self.__menubar = None in the constructor ?

      With what version of python do you get that message ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      U 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        What about using self.__menubar = None in the constructor ?

        With what version of python do you get that message ?

        U Offline
        U Offline
        user4592357
        wrote on last edited by
        #3

        @SGaist

        oh i forgot about that, thanks. and 3.6.3. isn't is normal that i get the error?

        JonBJ 1 Reply Last reply
        0
        • U user4592357

          @SGaist

          oh i forgot about that, thanks. and 3.6.3. isn't is normal that i get the error?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @user4592357
          PyCharm --- the IDE I use for Python/PyQt --- marks self. variables which are not created in __init__() with a harmless "warning underline". But it doesn't really matter. There is no "error message from Python".

          U 1 Reply Last reply
          0
          • JonBJ JonB

            @user4592357
            PyCharm --- the IDE I use for Python/PyQt --- marks self. variables which are not created in __init__() with a harmless "warning underline". But it doesn't really matter. There is no "error message from Python".

            U Offline
            U Offline
            user4592357
            wrote on last edited by
            #5

            @JonB

            i use visual studio code. anyways i think that initializing them in init isn't a bad idea, i was just confused about how to do it correctly

            JonBJ 1 Reply Last reply
            0
            • U user4592357

              @JonB

              i use visual studio code. anyways i think that initializing them in init isn't a bad idea, i was just confused about how to do it correctly

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @user4592357
              If you wish to ensure you always create the self. variables in __init__(), but that's before you are ready to actually create the objects --- e.g. you only create some member widget when the user performs some action --- then as @SGaist said do self.member = None in __init__(). That's effectively what happens for C++ code when member variables are declared (which of course you can't do in Python).

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Just a small correction, in C++, class members are not initialised by default, you have to explicitly set their value either in the constructor, constructor initialisation list or since C++11, you can do that at the same place as the declaration.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                JonBJ 1 Reply Last reply
                0
                • SGaistS SGaist

                  Just a small correction, in C++, class members are not initialised by default, you have to explicitly set their value either in the constructor, constructor initialisation list or since C++11, you can do that at the same place as the declaration.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @SGaist
                  Oh, so in C++, for example:

                  class Fred
                  {
                    private/protected/public:
                      char * m_pChar;
                  }
                  

                  m_pChar is not set to 0/NULL? (It is in C# :) )

                  U 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @SGaist
                    Oh, so in C++, for example:

                    class Fred
                    {
                      private/protected/public:
                        char * m_pChar;
                    }
                    

                    m_pChar is not set to 0/NULL? (It is in C# :) )

                    U Offline
                    U Offline
                    user4592357
                    wrote on last edited by
                    #9

                    @JonB

                    in c++ members get some value assigned to them if you don't init them explicitly. so in your case the pointer will have some garbage value, but not NULL/nullptr

                    1 Reply Last reply
                    1
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      No it's not.

                      See this excellent article from Arne Mertz on the subject.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved