Thread: Privacy

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

    Join Date
    Dec 2013
    Posts
    10
    Rep Power
    0

    Angry Privacy


    i'm reading about encapsulation and polymorphism in python. I was given an example:

    Code:
    c=name()
    c.setName('name')
    c.getName
    'name'
    privacy came into view in that I could

    Code:
    c=name()
    d=name()
    c.setName('name')
    d.setName('newname')
    c.getName
    'newname'
    another eample was given to show that this could be prevented

    Code:
    e=name()
    f=name()
    e.setName('name')
    f.setName('newestname')
    e.getName
    ('name')
    somehow through explaining what an object state should be, the example magically managed not to change e's name into f's name despite the fact that the examples were exactly the same.

    Needless to say I have a fuzzy understanding of what needs to be done in order to give an object a definitive state now, because the books example was so poor besides wording to me that I needed to give objects a state if I intended to reuse object names (despite possible issues with globals).

    the one thing that it did offer me that was invaluable was this question:

    Java supports 4 levels of privacy
    python inherently supports none, although it was said later in the example that __name would translate names to _objectname__name and _name would make them not findable with *. This to me seems to be some support for privacy, nonetheless,

    do these loopholes in privacy make Python a less secure coding language? could anyone possibly make that example a bit more clear for me above? I really don't understand what it means to give an object a state, except to say that within the def statement and inherent operations I need to make clear what the program does and doesn't do, and whether or not external variables should be changed by internal factoring within the program. This apparently is the best encapsulation that Python offers...something like that....

    a little help?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,894
    Rep Power
    481
    python depends on programmers being nice. The example you showed depends on the program in
    Code:
    class name:
    We'd need to see this code---probably both versions of it---to understand. "magically". No. Program logic takes place. This is knowable. As Ken Iverson said, "A component is something you understand. A system is made from components." I'm quite sure the example is at the component level.

    You can distribute .pyc and .pyo files. You don't have to distribute source, making programs a little harder to reverse engineer.

    Python has the name mangling feature that you demonstrated. It's not secure because the clever programmer can determine the mangled name and use it directly.
    Code:
    class c:
        __a = None
    
    print(dir(c))   # '_c__a'  appears in the listing
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    611
    Rep Power
    65
    It most likely should be
    c.getName()
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,894
    Rep Power
    481
    Response to post 3: Perhaps. I had thought getName is a property.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    10
    Rep Power
    0

    ...


    it is getName in the book

    but tbh it wasn't code. the reason the author is making things so confusing is that he keeps starting his examples in the middle of imaginary code, trusting in arbitrary explanations. He's done it on a few occassions or just as bad he relies on examples from three chapters previous and updates them without showing a step by step. hence I was quite confused, but I think I've figured it out some. I mean I knew that the name mangling feature could be directly manipulated if someone could figure it out, and I had decided it was obvious some logic was happening as well that he was failing to explain explicitly. the example didn't get any better and it really wasn't code, it was what I posted except different names.

    Thanks for your answers guys, sorry for such a dim question, I guess I should have let that one fly. I knew the guy was being obscure.

IMN logo majestic logo threadwatch logo seochat tools logo