Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Deep Python Encapsulation
Forum Updated to NodeBB v4.3 + New Features

Deep Python Encapsulation

Scheduled Pinned Locked Moved Solved C++ Gurus
11 Posts 4 Posters 1.2k 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.
  • Sachin BhattS Sachin Bhatt
    class Computer:
    
        def __init__(self):
            self.__maxprice = 900
            self.minvalue = 200
       
    
    c = Computer()
    #print(c.__maxprice) # this will throw an exception
    c.__maxprice = 1000 # hear am assigning the new value to my mangled variable
    print(c.__maxprice) # this will return the value 1000
    

    According to the encapsulation in python from here, __maxprice should not be accessible from outside, yet in the above example, I am able to edit and access the value.

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

    @Sachin-Bhatt
    Are you sure that self.__maxprice inside Computer actually accesses the same variable as the one set via c.__maxprice = 1000? Because the behavior you report would be explicable if in fact that call from the outside world creates a different variable.....

    Sachin BhattS 1 Reply Last reply
    1
    • JonBJ JonB

      @Sachin-Bhatt
      Are you sure that self.__maxprice inside Computer actually accesses the same variable as the one set via c.__maxprice = 1000? Because the behavior you report would be explicable if in fact that call from the outside world creates a different variable.....

      Sachin BhattS Offline
      Sachin BhattS Offline
      Sachin Bhatt
      wrote on last edited by
      #3

      @JonB Yes, ik I made a bad choice by using it. Thanks for the clarification. My query got resolved.

      1 Reply Last reply
      0
      • TomZT Offline
        TomZT Offline
        TomZ
        wrote on last edited by
        #4

        Thank you for asking, though. Very interesting behavior on the side of that language.

        Compared to C++ or similar language which forbids you and tells you your mistake, I think a shadowing variable like this is likely to cause many hours of developer time to get wasted.

        JonBJ 1 Reply Last reply
        0
        • TomZT TomZ

          Thank you for asking, though. Very interesting behavior on the side of that language.

          Compared to C++ or similar language which forbids you and tells you your mistake, I think a shadowing variable like this is likely to cause many hours of developer time to get wasted.

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

          @TomZ
          I do not disagree! I am not a Python expert, but I think what is/must be going on here is:

          • Python __something --- double underscore --- class variables are (apparently) "very private". More private that ones starting with a single underscore!

          • And I can only imagine that attempting to access them from the outside world --- c.__maxprice --- either throws a "not found" exception on read or on write actually creates a brand new __maxprice class variable, which "has the same name" as the private-private one but is not the same variable!?

          Compared to C++ or similar language which forbids you and tells you your mistake, I think a shadowing variable like this is likely to cause many hours of developer time to get wasted.

          As I have discovered, C++ does not error or warn you if you create a local variable in a class method whose name shadows a class member variable. It regards you as knowing what you are doing (C# would warn....). And we have spent many hours with questions on this forum which turn out to be totally unintended (or not understood) shadowing like this, such that I wish there were a C++ compiler option to warn about just this!

          J.HilkJ 1 Reply Last reply
          0
          • JonBJ JonB

            @TomZ
            I do not disagree! I am not a Python expert, but I think what is/must be going on here is:

            • Python __something --- double underscore --- class variables are (apparently) "very private". More private that ones starting with a single underscore!

            • And I can only imagine that attempting to access them from the outside world --- c.__maxprice --- either throws a "not found" exception on read or on write actually creates a brand new __maxprice class variable, which "has the same name" as the private-private one but is not the same variable!?

            Compared to C++ or similar language which forbids you and tells you your mistake, I think a shadowing variable like this is likely to cause many hours of developer time to get wasted.

            As I have discovered, C++ does not error or warn you if you create a local variable in a class method whose name shadows a class member variable. It regards you as knowing what you are doing (C# would warn....). And we have spent many hours with questions on this forum which turn out to be totally unintended (or not understood) shadowing like this, such that I wish there were a C++ compiler option to warn about just this!

            J.HilkJ Online
            J.HilkJ Online
            J.Hilk
            Moderators
            wrote on last edited by
            #6

            @JonB said in Deep Python Encapsulation:

            As I have discovered, C++ does not error or warn you if you create a local variable in a class method whose name shadows a class member variable

            it does, if you enable -Wshadow


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            JonBJ 1 Reply Last reply
            2
            • J.HilkJ J.Hilk

              @JonB said in Deep Python Encapsulation:

              As I have discovered, C++ does not error or warn you if you create a local variable in a class method whose name shadows a class member variable

              it does, if you enable -Wshadow

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

              @J-Hilk
              Interesting! But from https://stackoverflow.com/a/57079021 I don't see where it would warn about local variable shadowing member variable?? E.g. "Warn when a local variable shadows another local variable or parameter. ". I want to warn on shadowing a member variable (in a method), not a local or parameter one?

              J.HilkJ 1 Reply Last reply
              0
              • JonBJ JonB

                @J-Hilk
                Interesting! But from https://stackoverflow.com/a/57079021 I don't see where it would warn about local variable shadowing member variable?? E.g. "Warn when a local variable shadows another local variable or parameter. ". I want to warn on shadowing a member variable (in a method), not a local or parameter one?

                J.HilkJ Online
                J.HilkJ Online
                J.Hilk
                Moderators
                wrote on last edited by
                #8

                @JonB from gcc.gnu.org

                -Wshadow
                Warn whenever a local variable or type declaration shadows another variable, parameter, type, class member (in C++), or instance variable (in Objective-C) or whenever a built-in function is shadowed. Note that in C++, the compiler warns if a local variable shadows an explicit typedef, but not if it shadows a struct/class/enum. If this warning is enabled, it includes also all instances of local shadowing. This means that -Wno-shadow=local and -Wno-shadow=compatible-local are ignored when -Wshadow is used. Same as -Wshadow=global.

                so, it should


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                JonBJ 1 Reply Last reply
                1
                • J.HilkJ J.Hilk

                  @JonB from gcc.gnu.org

                  -Wshadow
                  Warn whenever a local variable or type declaration shadows another variable, parameter, type, class member (in C++), or instance variable (in Objective-C) or whenever a built-in function is shadowed. Note that in C++, the compiler warns if a local variable shadows an explicit typedef, but not if it shadows a struct/class/enum. If this warning is enabled, it includes also all instances of local shadowing. This means that -Wno-shadow=local and -Wno-shadow=compatible-local are ignored when -Wshadow is used. Same as -Wshadow=global.

                  so, it should

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

                  @J-Hilk
                  OK, so since -Wshadow is the same as -Wshadow=global they count member->local shadow as "global", I thought that would be something different.

                  I will bear -Wshadow in mind for gcc! Anything for MSVC?

                  J.HilkJ 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @J-Hilk
                    OK, so since -Wshadow is the same as -Wshadow=global they count member->local shadow as "global", I thought that would be something different.

                    I will bear -Wshadow in mind for gcc! Anything for MSVC?

                    J.HilkJ Online
                    J.HilkJ Online
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #10

                    @JonB said in Deep Python Encapsulation:

                    Anything for MSVC?

                    build it first with gcc/clang ?

                    🤷‍♂️


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    1 Reply Last reply
                    1
                    • TomZT Offline
                      TomZT Offline
                      TomZ
                      wrote on last edited by
                      #11

                      In QtCreator I have warnings from my linter (cppcheck) when I create local variables that shadow class-things (including method-names).

                      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