Encapsulation, inheritance and polymorphism
jeanpierreda at gmail.com
Tue Jul 17 12:03:48 CEST 2012
On Tue, Jul 17, 2012 at 4:45 AM, Lipska the Kat <lipska at lipskathekat.com> wrote:
> Is Python truly OO or is it just one way to use the language. I see some
> documentation relating to classes but nothing on instantiation .. in fact
> the documentation appears to say that classes are used in a static way e.g
> ClassName.method(), and command line scripting is really outside the scope
> of other OO languages I have experienced.
It doesn't look like you're reading the section on classes:
You create a class like this:
class MyClass(MySuperclass1, MySuperclass2):
# class attributes and methods
attr = 3
def method(self): print self.attr
You can instantiate it by calling the class, as if it were a function:
myinstance = MyClass()
You call a method getting the method (myinstance.method), and then
calling that as if it were a function.
Polymorphism is "duck typed" -- that is, it's based on the
presence/absence of attributes and methods, not on declared interfaces
(Python has no interfaces). So, for example, "foo.read()" works on any
object bound to the name "foo", as long as it has a read method that
takes no parameters. This could be a file-like object, but it could
also be something completely different that just happens to have a
method by the same name.
> Is there a previous discussion in the group that I could read.
Man, I dunno, the list archives are impossible to navigate.
More information about the Python-list