Encapsulation in Python

Mark Lawrence breamoreboy at yahoo.co.uk
Thu Mar 10 14:35:14 EST 2016


On 10/03/2016 14:57, Dan Strohl via Python-list wrote:
>> I've been studying Object Oriented Theory using Java. Theoretically, all
>> attributes should be private, meaning no one except the methods itself can
>> access the attribute;
>>
>> public class Foo {
>>      private int bar;
>>      ...
>

For the benefit of any newbies/lurkers I'll just point out that this 
might well be valid Java, but...

> Why?  I mean sure, lots of them should be, but if I am doing something like:
>
> class person:
>       age = 21
>       name = 'Cool Dude'
>

...this gives you class attributes, so the age is always 21 and the name 
is always 'Cool Dude'.  So you can vary the age and name you'd need:-

class person():
     def __init__(self, age, name):
         self.age = age
         self.name = name

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence




More information about the Python-list mailing list