[perl-python] 20050124 classes & objects

Discussion in 'Perl Misc' started by Xah Lee, Jan 24, 2005.

  1. Xah Lee

    Xah Lee Guest

    © # -*- coding: utf-8 -*-
    © # Python
    ©
    © # in Python, one can define a boxed set
    © # of data and functions, which are
    © # traditionally known as "class".
    ©
    © # in the following, we define a set of data
    © # and functions as a class, and name it xxx
    © class xxx:
    © "a class extempore! (^_^)"
    © i=1 # i'm a piece of data
    © def okaydokey(self): return "okaydokey"
    © def square(self,a): return a**a
    ©
    © # in the following,
    © # we create an object, of the class xxx.
    © # also known as "instantiate a class".
    © x = xxx()
    ©
    © # data or functions defined in a class
    © # are called the class's attributes or
    © # methods.
    © # to use them, append a dot and
    © # their name after the object's name.
    © print 'value of attribute i is:', x.i
    © print "3 squared is:", x.square(3)
    © print "okaydokey called:", x.okaydokey()
    ©
    © # in the definition of function inside a
    © # class, the first parameter "self" is
    © # necessary. (you'll know why when you need to)
    ©
    © # the first line in the class definition
    © # is the class's documentation. It can
    © # be accessed thru the __doc__
    © # attribute.
    © print "xxx's doc string is:", x.__doc__
    ©
    © # one can change data inside the class
    © x.i = 400
    ©
    © # one can also add new data to the class
    © x.j=4
    © print x.j
    ©
    © # or even override a method
    © x.square = 333
    © # (the following line will no longer work)
    © # print "3 squared is:", x.square(3)
    ©
    © # in Python, one must be careful not to
    © # overwrite data or methods defined in a
    © # class.

    ----------------------

    for a obfuscated treatment with a few
    extra info, see
    http://python.org/doc/2.3.4/tut/node11.html

    in Python terminal, type help() then
    topic CLASSES to read about existing
    datatypes as classes, and classes in
    Python

    try to write a class with one data of
    integer and two functions, one
    increases it by 1, one decreases it by
    1. note: inside a class definition,
    to refer to data inside itself use
    self. e.g. self.i

    ------------------------------------------
    Perl does not support classes or
    objects in the so-called "Object
    Oriented" programing. However, a
    complete set of emulations of OO
    style of programing have been done,
    resulting in modules and books and
    many documentations and tutorials.

    here is a quote from
    perldoc perlobj

    First you need to understand what
    references are in Perl. See perlref for
    that. Second, if you still find the
    following reference work too
    complicated, a tutorial on
    object-oriented programming in Perl can
    be found in perltoot and perltooc.

    it goes on and sayz:

    If you're still with us, then here are
    three very simple definitions that you
    should find reassuring.

    1. An object is simply a reference
    that happens to know which class
    it belongs to.

    2. A class is simply a package that
    happens to provide methods to deal
    with object references.

    3. A method is simply a subroutine
    that expects an object reference
    (or a package name, for class
    methods) as the first argument.

    Good luck.


    Note: this post is from the Perl-Python a-day mailing list at
    http://groups.yahoo.com/group/perl-python/
    to subscribe, send an email to perl-python-subscribe @ yahoogroups.com
    if you are reading it on a web page, program examples may not run
    because html conversion often breaks the code.
    Xah

    http://xahlee.org/PageTwo_dir/more.html
    Xah Lee, Jan 24, 2005
    #1
    1. Advertising

  2. Xah Lee wrote:

    > Perl does not support classes or
    > objects in the so-called "Object
    > Oriented" programing.


    Boy, the ignorance never stops, does it?

    > However, a
    > complete set of emulations of OO
    > style of programing have been done,
    > resulting in modules and books and
    > many documentations and tutorials.
    >


    It doesn't have OO, but it emulates in software!
    Better go with python, which has hardware OO. :)
    --
    Christopher Mattern

    "Which one you figure tracked us?"
    "The ugly one, sir."
    "...Could you be more specific?"
    Chris Mattern, Jan 24, 2005
    #2
    1. Advertising

  3. Chris Mattern wrote:

    >
    > It doesn't have OO, but it emulates in software!
    > Better go with python, which has hardware OO. :)


    Chris don't feed the troll
    Gian Mario Tagliaretti, Jan 24, 2005
    #3
    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. Carlo v. Dango
    Replies:
    14
    Views:
    1,031
    Alex Martelli
    Oct 19, 2003
  2. Bo Peng
    Replies:
    7
    Views:
    356
    Bo Peng
    Jun 5, 2004
  3. Xah Lee
    Replies:
    2
    Views:
    329
    Gian Mario Tagliaretti
    Jan 24, 2005
  4. 7stud
    Replies:
    11
    Views:
    689
    Dennis Lee Bieber
    Mar 20, 2007
  5. vasudevram
    Replies:
    6
    Views:
    924
    vasudevram
    Apr 24, 2013
Loading...

Share This Page