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 4 Feb 2018, 18:25 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
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 4 Feb 2018, 21:33 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 5 Feb 2018, 08:56
      1
      • S SGaist
        4 Feb 2018, 21:33

        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 5 Feb 2018, 08:56 last edited by
        #3

        @SGaist

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

        J 1 Reply Last reply 5 Feb 2018, 09:42
        0
        • U user4592357
          5 Feb 2018, 08:56

          @SGaist

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

          J Offline
          J Offline
          JonB
          wrote on 5 Feb 2018, 09:42 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 5 Feb 2018, 09:46
          0
          • J JonB
            5 Feb 2018, 09:42

            @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 5 Feb 2018, 09:46 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

            J 1 Reply Last reply 5 Feb 2018, 09:50
            0
            • U user4592357
              5 Feb 2018, 09:46

              @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

              J Offline
              J Offline
              JonB
              wrote on 5 Feb 2018, 09:50 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
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 5 Feb 2018, 11:10 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

                J 1 Reply Last reply 5 Feb 2018, 11:19
                0
                • S SGaist
                  5 Feb 2018, 11:10

                  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.

                  J Offline
                  J Offline
                  JonB
                  wrote on 5 Feb 2018, 11:19 last edited by JonB 2 May 2018, 11:21
                  #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 5 Feb 2018, 12:22
                  0
                  • J JonB
                    5 Feb 2018, 11:19

                    @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 5 Feb 2018, 12:22 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
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 5 Feb 2018, 21:30 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

                      1/10

                      4 Feb 2018, 18:25

                      • Login

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