Without using set_xx() and get_xx() methods

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • psbasha
    Contributor
    • Feb 2007
    • 440

    #1

    Without using set_xx() and get_xx() methods

    Hi ,

    Is it possible to simply the method access of a classes in Python ,without using set_xx() and get_xx() methods and maintaing the encapsulation functionality.

    Thanks in advance
    PSB
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by psbasha
    Hi ,

    Is it possible to simply the method access of a classes in Python ,without using set_xx() and get_xx() methods and maintaing the encapsulation functionality.

    Thanks in advance
    PSB
    The key phrase: "maintaing the encapsulation" dictates using getters and setters. Python allows you to violate the encapsulation freely.
    Code:
    class aClass:
        classVar = 1
    
    anInst = aClass()
    print anInst.classVar
    
    1
    It's just not strict OOP, that's all. The reason it's not recommended in strict OOP is that you can also
    Code:
    anInst.classVar = 2
    which can be hard to track down in a large system.

    Comment

    • psbasha
      Contributor
      • Feb 2007
      • 440

      #3
      But we can make the variable of a class private by declaring it as

      "__classVar " in a class.But in this case we have to use "get" and "set".

      So inorder to use "Encapsulat ion" we have to use "get" and "set"


      - PSB

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by psbasha
        But we can make the variable of a class private by declaring it as

        "__classVar " in a class.But in this case we have to use "get" and "set".

        So inorder to use "Encapsulat ion" we have to use "get" and "set"


        - PSB
        That's the idea, as I see it.

        Comment

        Working...