473,471 Members | 4,648 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

About classes and OOP in Python

Hello all,

I've been wondering a lot about why Python handles classes and OOP the
way it does. From what I understand, there is no concept of class
encapsulation in Python, i.e. no such thing as a private variable. Any
part of the code is allowed access to any variable in any class, and
even non-existant variables can be accessed: they are simply created.
I'm wondering what the philosophy behind this is, and if this
behaviour is going to change in any future release of Python.

It seems to me that it is difficult to use OOP to a wide extent in
Python code because these features of the language introduce many
inadvertant bugs. For example, if the programmer typos a variable name
in an assignment, the assignment will probably not do what the
programmer intended.

Ruby does something with this that I think would be excellent as an
inclusion in Python (with some syntax changes, of course). If private
variables require a setter/getter pair, we can shortcut that in some
way, i.e. (pseudocode):

class PythonClass:
private foo = "bar"
private var = 42
allow_readwrite( [ foo, var ] )

Or allow_read to only allow read-only access. Also there might be a
way to implement custom getters and setters for those times you want
to modify input or something:

class PythonClass:
def get foo():
return "bar"

def set var( value ):
var = value

Anyways, these are just some speculatory suggestions. My main question
is that of why Python chooses to use this type of OOP model and if it
is planned to change.

Thanks!

Apr 10 '06 #1
19 1382
You can do this in Python as well. Check out the property built-in
function. One can declare a property with a get, set, and delete
method. Here's a small example of a read-only property.

class Test(object):
def getProperty(self):
return 0;

prop = property(fget = getProperty)

t = Test()

print t.prop

t.prop = 100 # this will fail

Apr 10 '06 #2
"fyhuang" <fy*****@gmail.com> wrote:
I've been wondering a lot about why Python handles classes and OOP the
way it does. From what I understand, there is no concept of class
encapsulation in Python, i.e. no such thing as a private variable. Any
part of the code is allowed access to any variable in any class, and
even non-existant variables can be accessed: they are simply created.
I'm wondering what the philosophy behind this is, and if this
behaviour is going to change in any future release of Python.
There are advantages and disadvantages to C++/Java style encapsulation
using private data. The advantages you (apparently) already know. The
disadvantage is added complexity. There's a saying, "You can't have a bug
in a line of code you never write". By having to write all those getter
and setter methods, you just add bulk and complexity.

That being said, you can indeed have private data in Python. Just prefix
your variable names with two underscores (i.e. __foo), and they effectively
become private. Yes, you can bypass this if you really want to, but then
again, you can bypass private in C++ too. You can also intercept any
attempt to access Python attributes by writing __getattr__() and
__setattr__() methods for your class.
It seems to me that it is difficult to use OOP to a wide extent in
Python code because these features of the language introduce many
inadvertant bugs. For example, if the programmer typos a variable name
in an assignment, the assignment will probably not do what the
programmer intended.
Yes, that is is a risk. Most people deal with that risk by doing a lot of
testing (which you should be doing anyway). If you really want to, you can
use the __slots__ technique to prevent this particular bug from happening
(although the purists will tell you that this is not what __slots__ was
designed for).
My main question is that of why Python chooses to use this type of OOP
model and if it is planned to change.


It sounds like you are used to things like C++ and Java, which are very
static languages. Everything is declared at compile time, and there are
many safeguards in the language to keep you from shooting yourself in the
foot. They problem is, they often prevent you from getting any useful work
done either; you spend most of your time programming the language, not the
problem you are trying to solve.

In the past week, I've had two conversations with people about the nuances
of C++ assignment operators. None of our customers give two figs about
assignment operators. Getting them right is just a detour we need to take
to keep our software from crashing. With Python, I write a = b and trust
that it does the right thing. That lets me concentrate on adding value
that our customer will see.
Apr 10 '06 #3
Em Seg, 2006-04-10 Ã*s 07:19 -0700, fyhuang escreveu:
class PythonClass:
private foo = "bar"
private var = 42
allow_readwrite( [ foo, var ] )
You are aware that foo and var would become class-variables, not
instance-variables, right?

But you can always do:

class PythonClass(object):
def __init__(self):
self.__foo = "bar"

foo = property(lambda self: self.__foo)
And then:
a = PythonClass()
a.foo 'bar' a.foo = 'baz' Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: can't set attribute
But you can also bypass this "security":
a._PythonClass__foo = 'baz'
a.foo

'baz'
But this was not a mistake, nobody mistakenly writes "_PythonClass__".
Or allow_read to only allow read-only access. Also there might be a
way to implement custom getters and setters for those times you want
to modify input or something:

class PythonClass:
def get foo():
return "bar"

def set var( value ):
var = value


There's a PEP somewhere that proposes things like (same example I gave
earlier):

class PythonClass(object):
def __init__(self):
self.__foo = "bar"

create property foo:
def get(self):
return self.__foo

--
Felipe.

Apr 10 '06 #4
fyhuang <fy*****@gmail.com> wrote:
[ ... ] no such thing as a private variable. Any
part of the code is allowed access to any variable in any class, and
even non-existant variables can be accessed: they are simply created.
You're confusing two issues: encapsulation and dynamic name binding.
You might also want to check out the difference between read and
write access to non-existant variables.
I'm wondering what the philosophy behind this is,
"We are all consenting adults." And probably experience of data
encapsulation being more of a hinderence than a benefit.
and if this
behaviour is going to change in any future release of Python.


I damn well hope not.

And if you want to control those writes, try this for a base class:

class restrict_set:
def __init__(self, allowed_names):
self._allowed_names = set(allowed_names)
def __setattr__(self, name, value):
if not name.startswith('_') and name not in self._allowed_names:
getattr(self, name)
self.__dict__[name] = value

(Someone else can do the new style class and the metaclass versions.)

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
Apr 10 '06 #5
Hi,

fyhuang schrieb:
I've been wondering a lot about why Python handles classes and OOP the
way it does. From what I understand, there is no concept of class
encapsulation in Python, i.e. no such thing as a private variable. Any


the answer is here:

http://tinyurl.com/obgho

--
Mit freundlichen Grüßen,
Ing. Gregor Horvath, Industrieberatung & Softwareentwicklung
http://www.gregor-horvath.com
Apr 10 '06 #6
fyhuang wrote:
It seems to me that it is difficult to use OOP to a wide extent in
Python code because these features of the language introduce many
inadvertant bugs. For example, if the programmer typos a variable name
in an assignment, the assignment will probably not do what the
programmer intended.


You'll find that if you assign to a wrongly-typed variable name, then
later attempts to access the variable you wrongly believed you typed
will raise an exception. If you assign from a wrongly-typed variable,
again an exception will be raised.

I think it's important not to wrongly confuse 'OOP' with ''data hiding'
or any other aspect you may be familiar with from Java or C++. The
primary concept behind OOP is not buzzwords such as abstraction,
encapsulation, polymorphism, etc etc, but the fact that your program
consists of objects maintaining their own state, working together to
produce the required results, as opposed to the procedural method where
the program consists of functions that operate on a separate data set.

--
Ben Sizer

Apr 11 '06 #7
Ben Sizer wrote:
I think it's important not to wrongly confuse 'OOP' with ''data hiding'
or any other aspect you may be familiar with from Java or C++. The
primary concept behind OOP is not buzzwords such as abstraction,
encapsulation, polymorphism, etc etc, but the fact that your program
consists of objects maintaining their own state, working together to
produce the required results, as opposed to the procedural method where
the program consists of functions that operate on a separate data set.


+1 QOTW

</F>

Apr 11 '06 #8
Roy Smith wrote:
<snip>
That being said, you can indeed have private data in Python. Just prefix
your variable names with two underscores (i.e. __foo), and they effectively
become private. Yes, you can bypass this if you really want to, but then
again, you can bypass private in C++ too.


Wrong, _foo is a *private* name (in the sense "don't touch me!"), __foo
on the contrary is a *protected* name ("touch me, touch me, don't worry
I am protected against inheritance!").
This is a common misconception, I made the error myself in the past.

Michele Simionato

Apr 11 '06 #9
fyhuang wrote:
Hello all,

I've been wondering a lot about why Python handles classes and OOP the
way it does. From what I understand, there is no concept of class
encapsulation in Python, i.e. no such thing as a private variable.
Seems you're confusing encapsulation with data hiding.
Any
part of the code is allowed access to any variable in any class,
This is also true for Java and C++ - it just a requires a little bit
more language-specific knowledge.

Python relies a lot on conventions. One of these conventions is that any
attribute whose name begins with an underscore is implementation detail
and *should* not be accessed from client code.
and
even non-existant variables can be accessed:
Nope. You can dynamically *add* new attributes - either to an instance
or a class - but trying to *read* a non-existant attribute will raise an
AttributeError.
they are simply created.
I'm wondering what the philosophy behind this is,
Dynamism.
and if this
behaviour is going to change in any future release of Python.
Certainly not.
It seems to me that it is difficult to use OOP to a wide extent in
Python code because these features of the language introduce many
inadvertant bugs.
Don't assume, verify. My own experience is that it's *much more* easy to
do OOP with a dynamic language.
For example, if the programmer typos a variable name
in an assignment, the assignment will probably not do what the
programmer intended.
That's true for any language. I never had any serious problem with this
in 5+ years - not that I'm never making typos, but it never took me more
than a pair of minutes to spot and correct this kind of errors. OTOH,
Python's dynamism let me solved in a quick and clean way problems that
would have been a royal PITA in some less agile languages.
Ruby does something with this that I think would be excellent as an
inclusion in Python (with some syntax changes, of course). If private
variables require a setter/getter pair, we can shortcut that in some
way, i.e. (pseudocode):

class PythonClass:
private foo = "bar"
private var = 42
allow_readwrite( [ foo, var ] )
A first point: in Ruby (which closely follows Smalltalk's model),
there's a definitive distinction between callable and non-callable
attributes, and this last category is *always* private.

A second point is that Python also provides you getter/setter for
attributes. The default is read/write, but you can easily make any
attribute read-only (or write-only FWIW) and add any computation.
Or allow_read to only allow read-only access. Also there might be a
way to implement custom getters and setters for those times you want
to modify input or something:

class PythonClass:
def get foo():
return "bar"

def set var( value ):
var = value
What you want is named "property".
Anyways, these are just some speculatory suggestions.
Don't waste time with speculations. Read the Fine Manual instead, and
actually *use* Python.
My main question
is that of why Python chooses to use this type of OOP model and if it
is planned to change.


I'm not the BDFL, but my bet is that this will *not* change.

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Apr 11 '06 #10
Roy Smith wrote:
(snip)
That being said, you can indeed have private data in Python. Just prefix
your variable names with two underscores (i.e. __foo), and they effectively
become private.
The double-leading-underscore stuff has nothing to do with "privacy".
It's meant to protect from *accidental* overriding of implementation stuff.

(snip)
Yes, that is is a risk. Most people deal with that risk by doing a lot of
testing (which you should be doing anyway). If you really want to, you can
use the __slots__ technique to prevent this particular bug from happening
(although the purists will tell you that this is not what __slots__ was
designed for).


Ok, so I must be a purist !-)

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Apr 11 '06 #11
>I think it's important not to wrongly confuse 'OOP' with ''data hiding'
or any other aspect you may be familiar with from Java or C++. The
primary concept behind OOP is not buzzwords such as abstraction,
encapsulation, polymorphism, etc etc, but the fact that your program
consists of objects maintaining their own state, working together to
produce the required results, as opposed to the procedural method where
the program consists of functions that operate on a separate data set.


Isn't "inheritance" an important buzzword for OOP?
--
Regards,
Casey
Apr 11 '06 #12
On 2006-04-11, Michele Simionato <mi***************@gmail.com> wrote:
Roy Smith wrote:
<snip>
That being said, you can indeed have private data in Python. Just prefix
your variable names with two underscores (i.e. __foo), and they effectively
become private. Yes, you can bypass this if you really want to, but then
again, you can bypass private in C++ too.
Wrong, _foo is a *private* name (in the sense "don't touch me!"), __foo
on the contrary is a *protected* name ("touch me, touch me, don't worry
I am protected against inheritance!").
This is a common misconception, I made the error myself in the past.


Please explain! I didn't think _foo meant anything special, __foo
expands to _classname__foo for some sort of name-hiding. What am I
missing?
Apr 11 '06 #13
On Tue, 11 Apr 2006 18:20:13 +0000, Casey Hawthorne wrote:
I think it's important not to wrongly confuse 'OOP' with ''data hiding'
or any other aspect you may be familiar with from Java or C++. The
primary concept behind OOP is not buzzwords such as abstraction,
encapsulation, polymorphism, etc etc, but the fact that your program
consists of objects maintaining their own state, working together to
produce the required results, as opposed to the procedural method where
the program consists of functions that operate on a separate data set.


Isn't "inheritance" an important buzzword for OOP?


Of course inheritance is an important and desirable feature of OOP, but it
isn't a necessary feature. Python built-in objects like int, list etc.
were still objects even before you could inherit from them.

I don't know of many other OO languages that didn't/don't have
inheritance, but there was at least one: Apple's Hypertalk, back in the
late 80s early 90s.

--
Steven.

Apr 12 '06 #14
Michele Simionato wrote:
Roy Smith wrote:
<snip>
That being said, you can indeed have private data in Python. Just prefix
your variable names with two underscores (i.e. __foo), and they effectively
become private. Yes, you can bypass this if you really want to, but then
again, you can bypass private in C++ too.


Wrong, _foo is a *private* name (in the sense "don't touch me!"), __foo
on the contrary is a *protected* name ("touch me, touch me, don't worry
I am protected against inheritance!").
This is a common misconception, I made the error myself in the past.


Sure, if you only consider "private" and "protected" as they're defined
in a dictionary. But then you'd be ignoring the meanings of the
public/private/protected keywords in virtually every language that has
them. http://www.google.com/search?q=public+private+protected

Python doesn't have these keywords, but most Python programmers are at
least somewhat familiar with a language that does use them. For the
sake of clarity:
__foo ~= private = used internally by base class only
_foo ~= protected = used internally by base and derived classes

The Python docs use the above definitions.

--Ben

Apr 12 '06 #15
Steven D'Aprano schrieb:
I don't know of many other OO languages that didn't/don't have
inheritance,


VB4 - VB6

--
Mit freundlichen Grüßen,
Ing. Gregor Horvath, Industrieberatung & Softwareentwicklung
http://www.gregor-horvath.com
Apr 12 '06 #16
Ben C wrote:
On 2006-04-11, Michele Simionato <mi***************@gmail.com> wrote:
Roy Smith wrote:
<snip>
That being said, you can indeed have private data in Python. Just prefix
your variable names with two underscores (i.e. __foo), and they effectively
become private. Yes, you can bypass this if you really want to, but then
again, you can bypass private in C++ too.

Wrong, _foo is a *private* name (in the sense "don't touch me!"), __foo
on the contrary is a *protected* name ("touch me, touch me, don't worry
I am protected against inheritance!").
This is a common misconception, I made the error myself in the past.

Please explain! I didn't think _foo meant anything special,


It doesn't mean anything special in the language itself - it's a
convention between programmers. Just like ALL_CAPS names is a convention
for (pseudo) symbolic constants. Python relies a lot on conventions.
__foo
expands to _classname__foo for some sort of name-hiding.
s/hiding/mangling/
What am I
missing?


the __name_mangling mechanism is meant to protect some attributes to be
*accidentaly* overridden. It's useful for classes meant to be subclassed
(ie in a framework). It has nothing to do with access restriction - you
still can access such an attribute.

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Apr 12 '06 #17
Casey Hawthorne wrote:
I think it's important not to wrongly confuse 'OOP' with ''data hiding'
or any other aspect you may be familiar with from Java or C++. The
primary concept behind OOP is not buzzwords such as abstraction,
encapsulation, polymorphism, etc etc, but the fact that your program
consists of objects maintaining their own state, working together to
produce the required results, as opposed to the procedural method where
the program consists of functions that operate on a separate data set.

Isn't "inheritance" an important buzzword for OOP?


Which kind of inheritance ? subtyping or implementation inheritance ?-)

FWIW, subtyping is implicit in dynamically typed languages, so they
don't need support for such a mechanism. And implementation inheritance
is not much more than a special case of composition/delegation, so it's
almost useless in a language that have a good support for delegation
(which we have in Python, thanks to __getattr__/__setattr__).
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Apr 12 '06 #18
Michele Simionato wrote:
Roy Smith wrote:
<snip>
That being said, you can indeed have private data in Python. Just prefix
your variable names with two underscores (i.e. __foo), and they effectively
become private. Yes, you can bypass this if you really want to, but then
again, you can bypass private in C++ too.


Wrong, _foo is a *private* name (in the sense "don't touch me!"), __foo
on the contrary is a *protected* name ("touch me, touch me, don't worry
I am protected against inheritance!").
This is a common misconception, I made the error myself in the past.


While your wording makes sense, it's probably confusing for anyone
with a C++ background, where private roughly means "only accessible
within the actual class" and protected roughly means "only accessible
within the class and other classes derived from it".
Apr 12 '06 #19
Gregor Horvath wrote:
Steven D'Aprano schrieb:

I don't know of many other OO languages that didn't/don't have
inheritance,

VB4 - VB6

VB6 has a kind of inheritance via interface/delegation. The interface
part is for subtyping, the delegation part (which has to be done
manually - yuck) is for implementation inheritance. Needless to say it's
a king-size PITA...

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Apr 12 '06 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
39
by: Marco Aschwanden | last post by:
Hi I don't have to talk about the beauty of Python and its clear and readable syntax... but there are a few things that striked me while learning Python. I have collected those thoughts. I am...
5
by: b-blochl | last post by:
I wonder about the huge traffic in the question how many tuples are dispensible. I find that a question of only ternary or quarterny order - use it or use it not (free after Shakespears "to be or...
28
by: David MacQuigg | last post by:
I'm concerned that with all the focus on obj$func binding, &closures, and other not-so-pretty details of Prothon, that we are missing what is really good - the simplification of classes. There are...
3
by: arserlom | last post by:
Hello I have a question about inheritance in Python. I'd like to do something like this: class cl1: def __init__(self): self.a = 1 class cl2(cl1): def __init__(self): self.b = 2
97
by: Cameron Laird | last post by:
QOTW: "Python makes it easy to implement algorithms." - casevh "Most of the discussion of immutables here seems to be caused by newcomers wanting to copy an idiom from another language which...
161
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.....
3
by: redefined.horizons | last post by:
I've been reading about Python Classes, and I'm a little confused about how Python stores the state of an object. I was hoping for some help. I realize that you can't create an empty place holder...
19
by: adriancico | last post by:
Hi I am working on a python app, an outliner(a window with a TreeCtrl on the left to select a document, and a RichTextBox at the right to edit the current doc). I am familiarized with OOP...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.