Python

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • OOP and Python.. a gentle remark

    8 answers - 661 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    Folks
    Please dont take it in a wrong sense. But I have a point to make regarding
    P and the way it is implemented in Python.
    Why is it necessary to explicity use self argument in the class functions
    ? I feel the language/interpreter should figure out which object has called
    the function? Isnt it ? (the use of 'self' keyword really confuses me. and
    to make matter worse the variables can be accessed from outside teh class.
    Isnt it a violation of the P principle of ENCAPSULATIN)
    Also please let me know hwo can we declare PRIVATE VARIABLES in Python?
    Regards,
    Asrarahmed
    P.S. : I love Python. :)-
  • No.1 | | 692 bytes | |

    Asrarahmed Kadri :

    Folks

    Please dont take it in a wrong sense. But I have a point to make
    regarding P and the way it is implemented in Python.

    Why is it necessary to explicity use self argument in the class
    functions ? I feel the language/interpreter should figure out which
    object has called the function? Isnt it ? (the use of 'self' keyword
    really confuses me. and to make matter worse the variables can be
    accessed from outside teh class. Isnt it a violation of the P
    principle of ENCAPSULATIN)

    Also please let me know hwo can we declare PRIVATE VARIABLES in
    Python?

    Regards,
    Asrarahmed

    P.S. : I love Python. :)-
  • No.2 | | 613 bytes | |

    10/25/06, Asrarahmed Kadri <ajkadri (AT) googlemail (DOT) comwrote:
    Why is it necessary to explicity use self argument in the class functions
    ? I feel the language/interpreter should figure out which object has called
    the function? Isnt it ? (the use of 'self' keyword really confuses me. and
    to make matter worse the variables can be accessed from outside teh class.
    Isnt it a violation of the P principle of ENCAPSULATIN)

    <http://tinyurl.com/y5mcea>

    Also please let me know hwo can we declare PRIVATE VARIABLES in Python?

    <http://tinyurl.com/y5f2nz>
  • No.3 | | 2703 bytes | |

    Asrarahmed Kadri <ajkadri <atgooglemail.comwrites:

    the use of 'self' keyword really confuses me.

    I see how it can annoy you, but not how it can *confuse* you - if anything,
    "self" removes any confusion about whether you're looking at a local variable or
    an object property. By the way, the use of special identifiers is not uncommon
    in dynamic languages. Ruby for example uses the prefixes @, @@ and $ to define
    variable scope (which are not particularly self-explanatory - I think they're
    class, instance and global vars) and keywords (private, protected, public) to
    define method visibility.

    and to make matter worse the variables can be accessed from outside teh class.
    Isnt it a violation of the P principle of ENCAPSULATIN)

    Python has conventions for this: prepend _ for protected and __ for private
    (private names get mangled). In an interactive session:

    class a(object):
    def _protected(s): # don't *have* to use "self" :)
    print s
    def __private(s):
    print "private"
    A = a()
    A._protected()
    <__maina object at 0x00AF2E10>
    Aprivate()
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    AttributeError: 'a' object has no attribute '__private'

    Lets's examine what's happened:

    dir(A)
    [<snip>, '_a__private', '_protected']

    See, __private was mangled to _a__private. We can still access it of course:

    A._a__private()
    private

    Python approaches the programmer as a responsible person: if I see a method name
    is mangled (or marked with _), I know I'm not supposed to use it - but can use
    it if I really need to (and suffer the consequences if the next version of the
    class breaks my code).
    languages choose the opposite approach: private is private, don't trust
    the programmer, tough luck if you really really need access to that private.
    Unfortunately you will invariably encounter cases when you need to access
    privates and these are major PITA's. Usually you can get your hands on the
    secret stuff, it's just very annoying and silly (e.g. by creating shadow classes
    or other workarounds).

    In the end it comes down to a choice between making some unavoidable cases very
    cumbersome to deal with, or trusting the programmer to do the right thing -
    thereby making it easier to stick his grubby little fingers in places where he
    shouldn't. Some people prefer the former, some the latter.

    Yours,

    Andrei

    Tutor maillist - Tutor (AT) python (DOT) org
  • No.4 | | 2550 bytes | |

    "Asrarahmed Kadri" <ajkadri (AT) googlemail (DOT) comwrote
    Why is it necessary to explicity use self argument in the class
    functions

    Because Guido made it that way. :-)
    But he did it for good reasons which others have pointed out already.

    Although languages like C++ and Java use implicit object references,
    you can make them explicit with the 'this' keyword and most industrial
    coding standards mandate that 'this' be used when referencing class
    level variables inside methods. As in so many things Python simply
    takes best practice and makes it mandatory.

    the function? Isnt it ? (the use of 'self' keyword really confuses
    me. and

    If its the actual term 'self' then you can change it to anything
    you prefer. C++/Java programmers might prefer 'this', some
    others have used 'my':

    class C:
    def __init__(my, aValue):
    my.value = aValue

    is perfectly valid Python.

    to make matter worse the variables can be accessed from outside teh
    class.
    Isnt it a violation of the P principle of ENCAPSULATIN)

    The original P concept of encapsulation refers to the ability
    to bind functions and data together in a single unit - a class or
    object - and had nothing to do with data hiding which is the ability
    to conceal the implementation details behind an API.

    It was only after C++ came out with its gazillion access
    controls (public, private, protected, friend etc) that people started
    to confuse data hiding and encapsulation. Many early P
    languages (most Lisps included) do not offer explicit or strict
    data hiding.

    Also please let me know hwo can we declare
    PRIVATE VARIABLES in Python?

    Why do you think you need them?
    Do you frequently hit bugs because you couldn't resist the
    urge to access members directly? While there can be problems
    with badly behaved programmers on large projects, in the
    things Python is typically used for its very rarely a problem.

    And it can add significantly to the complexity of the code
    when you try to derive a class from one with private data.
    Very few designers are sufficiently ppsychic to predict
    exactly how all future users will want to modify their class!
    The result is they supply get/set methods for all data which
    are simply pass through methods. In doing so they
    completely annull any benefit of private members and
    introduce potential performance problems.

    HTH,
  • No.5 | | 2829 bytes | |

    I dont know about the rest of you, but this thread posting from Alan cleared
    up some fuzzyness I've had about classes. /me is happier using this. instead
    of self. =D

    Great post!

    10/25/06, Alan Gauld <alan.gauld (AT) btinternet (DOT) comwrote:
    --
    "Asrarahmed Kadri" <ajkadri (AT) googlemail (DOT) comwrote
    Why is it necessary to explicity use self argument in the class
    functions

    Because Guido made it that way. :-)
    But he did it for good reasons which others have pointed out already.

    Although languages like C++ and Java use implicit object references,
    you can make them explicit with the 'this' keyword and most industrial
    coding standards mandate that 'this' be used when referencing class
    level variables inside methods. As in so many things Python simply
    takes best practice and makes it mandatory.

    the function? Isnt it ? (the use of 'self' keyword really confuses
    me. and

    If its the actual term 'self' then you can change it to anything
    you prefer. C++/Java programmers might prefer 'this', some
    others have used 'my':

    class C:
    def __init__(my, aValue):
    my.value = aValue

    is perfectly valid Python.

    to make matter worse the variables can be accessed from outside teh
    class.
    Isnt it a violation of the P principle of ENCAPSULATIN)

    The original P concept of encapsulation refers to the ability
    to bind functions and data together in a single unit - a class or
    object - and had nothing to do with data hiding which is the ability
    to conceal the implementation details behind an API.

    It was only after C++ came out with its gazillion access
    controls (public, private, protected, friend etc) that people started
    to confuse data hiding and encapsulation. Many early P
    languages (most Lisps included) do not offer explicit or strict
    data hiding.

    Also please let me know hwo can we declare
    PRIVATE VARIABLES in Python?

    Why do you think you need them?
    Do you frequently hit bugs because you couldn't resist the
    urge to access members directly? While there can be problems
    with badly behaved programmers on large projects, in the
    things Python is typically used for its very rarely a problem.

    And it can add significantly to the complexity of the code
    when you try to derive a class from one with private data.
    Very few designers are sufficiently ppsychic to predict
    exactly how all future users will want to modify their class!
    The result is they supply get/set methods for all data which
    are simply pass through methods. In doing so they
    completely annull any benefit of private members and
    introduce potential performance problems.

    HTH,
  • No.6 | | 2693 bytes | |

    Thanks a lot for explanation.

    Regards,
    Asrarahmed

    10/25/06, Alan Gauld <alan.gauld (AT) btinternet (DOT) comwrote:
    --
    "Asrarahmed Kadri" <ajkadri (AT) googlemail (DOT) comwrote
    Why is it necessary to explicity use self argument in the class
    functions

    Because Guido made it that way. :-)
    But he did it for good reasons which others have pointed out already.

    Although languages like C++ and Java use implicit object references,
    you can make them explicit with the 'this' keyword and most industrial
    coding standards mandate that 'this' be used when referencing class
    level variables inside methods. As in so many things Python simply
    takes best practice and makes it mandatory.

    the function? Isnt it ? (the use of 'self' keyword really confuses
    me. and

    If its the actual term 'self' then you can change it to anything
    you prefer. C++/Java programmers might prefer 'this', some
    others have used 'my':

    class C:
    def __init__(my, aValue):
    my.value = aValue

    is perfectly valid Python.

    to make matter worse the variables can be accessed from outside teh
    class.
    Isnt it a violation of the P principle of ENCAPSULATIN)

    The original P concept of encapsulation refers to the ability
    to bind functions and data together in a single unit - a class or
    object - and had nothing to do with data hiding which is the ability
    to conceal the implementation details behind an API.

    It was only after C++ came out with its gazillion access
    controls (public, private, protected, friend etc) that people started
    to confuse data hiding and encapsulation. Many early P
    languages (most Lisps included) do not offer explicit or strict
    data hiding.

    Also please let me know hwo can we declare
    PRIVATE VARIABLES in Python?

    Why do you think you need them?
    Do you frequently hit bugs because you couldn't resist the
    urge to access members directly? While there can be problems
    with badly behaved programmers on large projects, in the
    things Python is typically used for its very rarely a problem.

    And it can add significantly to the complexity of the code
    when you try to derive a class from one with private data.
    Very few designers are sufficiently ppsychic to predict
    exactly how all future users will want to modify their class!
    The result is they supply get/set methods for all data which
    are simply pass through methods. In doing so they
    completely annull any benefit of private members and
    introduce potential performance problems.

    HTH,
  • No.7 | | 3009 bytes | |

    Chris, you are right.
    He (Alan) hits the bull's eye, always !!

    Cheers

    Asrarahmed

    10/25/06, Chris Hengge <pyro9219 (AT) gmail (DOT) comwrote:

    I dont know about the rest of you, but this thread posting from Alan
    cleared up some fuzzyness I've had about classes. /me is happier using this.
    instead of self. =D

    Great post!

    10/25/06, Alan Gauld <alan.gauld (AT) btinternet (DOT) comwrote:
    --
    "Asrarahmed Kadri" <ajkadri (AT) googlemail (DOT) comwrote
    Why is it necessary to explicity use self argument in the class
    functions

    Because Guido made it that way. :-)
    But he did it for good reasons which others have pointed out already.

    Although languages like C++ and Java use implicit object references,
    you can make them explicit with the 'this' keyword and most industrial
    coding standards mandate that 'this' be used when referencing class
    level variables inside methods. As in so many things Python simply
    takes best practice and makes it mandatory.

    the function? Isnt it ? (the use of 'self' keyword really confuses
    me. and

    If its the actual term 'self' then you can change it to anything
    you prefer. C++/Java programmers might prefer 'this', some
    others have used 'my':

    class C:
    def __init__(my, aValue):
    my.value = aValue

    is perfectly valid Python.

    to make matter worse the variables can be accessed from outside teh
    class.
    Isnt it a violation of the P principle of ENCAPSULATIN)

    The original P concept of encapsulation refers to the ability
    to bind functions and data together in a single unit - a class or
    object - and had nothing to do with data hiding which is the ability
    to conceal the implementation details behind an API.

    It was only after C++ came out with its gazillion access
    controls (public, private, protected, friend etc) that people started
    to confuse data hiding and encapsulation. Many early P
    languages (most Lisps included) do not offer explicit or strict
    data hiding.

    Also please let me know hwo can we declare
    PRIVATE VARIABLES in Python?

    Why do you think you need them?
    Do you frequently hit bugs because you couldn't resist the
    urge to access members directly? While there can be problems
    with badly behaved programmers on large projects, in the
    things Python is typically used for its very rarely a problem.

    And it can add significantly to the complexity of the code
    when you try to derive a class from one with private data.
    Very few designers are sufficiently ppsychic to predict
    exactly how all future users will want to modify their class!
    The result is they supply get/set methods for all data which
    are simply pass through methods. In doing so they
    completely annull any benefit of private members and
    introduce potential performance problems.

    HTH,
  • No.8 | | 426 bytes | |

    on a related topic, if you're concerned about security, esp. of your
    instance attributes, new-style classes offer you significantly more
    control over them using descriptors (including properties [and to a
    lesser extent, slots]). there is plenty of docs available on those, so
    i'll defer describing them here.

    good luck!
    -wesley

    Tutor maillist - Tutor (AT) python (DOT) org

Re: OOP and Python.. a gentle remark


max 4000 letters.
Your nickname that display:
In order to stop the spam: 4 + 3 =
QUESTION ON "Python"

EMSDN.COM