#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    3

    Encapsulation in python?


    could it be that there's no encapsulation in python?
    If I can access data members from main - what's the use in get and set functions?

    another question about the syntax:
    I noticed that python interpeter doesn't care if i end the line with ";" or not, what is the convention about it?
    should I end code lines with ";" or not?

    Thanks in advanced!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Originally Posted by so.very.tired
    If I can access data members from main - what's the use in get and set functions?
    Sometimes you need to ensure that internal variables are of a certain type, or follow a specific format. Then I would use a set function to aid other functions or implementations to use my code correctly.

    Originally Posted by so.very.tired
    another question about the syntax:
    I noticed that python interpeter doesn't care if i end the line with ";" or not, what is the convention about it?
    should I end code lines with ";" or not?
    Semicolons allow multiple statements on the same line, but are not mandatory to use. Personally I never use semicolons unless I'm running quick python hacks from the command line.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    Yes, it's hard to hide variables, functions, and methods in a python code. No question, encapsulation in python depends on programmers actually wanting to write maintainable programs that work.

    Sometimes there is little difference between "knowing and using an implementation detail" and "using get/set method." I guess it depends on the published interface.

    import math
    math.pi # not math.getpi()

    and as partoj said, if there's any complexity with type checking or range validation then encapsulation limits the code change to one spot. You don't have to search a million lines of code for "99" as the millennium terminates.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Code:
    >>> import math
    >>> math.pi
    3.1415926535897931
    >>> math.pi = int(3)    # Too many decimals, man!
    >>> math.pi
    3
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    3
    Thanks guys.
    that was very helpful.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    4
    Strictly speaking, Python doesn't have true encapsulation. In practice, this doesn't matter as long as you aren't an idiot. Everything is "public", but putting an underscore at the front of a method or attribute name is an accepted convention to signify that something is meant for internal use and that it should not be used by external callers unless they know what they are doing and do so at their own risk. In addition, automatic documentation generators (including the built-in help() function) usually don't display underscore-prefixed methods.

    Don't use getters and setters; they just make your code longer. Make instance variables part of the public interface when you want simple data access, and use properties if you need to do some validation or want a read-only attribute.

IMN logo majestic logo threadwatch logo seochat tools logo