Re: Encapsulation, inheritance and polymorphism

Discussion in 'Python' started by Devin Jeanpierre, Jul 17, 2012.

  1. On Tue, Jul 17, 2012 at 4:45 AM, Lipska the Kat <> 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:

    http://docs.python.org/tutorial/classes.html

    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.

    myinstance.method()

    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.

    -- Devin
    Devin Jeanpierre, Jul 17, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ulrich Eckhardt
    Replies:
    72
    Views:
    912
    Mark Lawrence
    Aug 19, 2012
  2. Terry Reedy
    Replies:
    0
    Views:
    186
    Terry Reedy
    Jul 17, 2012
  3. MRAB
    Replies:
    0
    Views:
    187
  4. Tim Chase
    Replies:
    0
    Views:
    154
    Tim Chase
    Jul 17, 2012
  5. Arnaud Delobelle
    Replies:
    0
    Views:
    153
    Arnaud Delobelle
    Jul 18, 2012
Loading...

Share This Page