Need help with OOP

Icarus Sparry usenet at icarus.freeuk.com
Fri Nov 7 01:47:20 EST 2003


sinisam wrote:

> Object oriented programming is not a new term to me, but when I tried
> to do some of it... uhh... it sure looked (and felt) like Hell :) I
> won't bother about things like "help me learn it" (but wouldn't mind
> if someone recommends a good fresh-start tutorial), instead I'll ask
> for help...
 
> I did my share of programming several years ago... 6 or 7 to be more
> specific, and as I'm pretty proud of my achievements ;)
> I continued on the same path. Several weeks ago, Python showed up.
> I got the 'assignment' to start learning it... almost professionally :)
> 
> OK, I did my part, but now the tests are becoming more than fair :(
> 
> Next part (even though I said I know *nothing* about OOP) is:
> 
> "Make class for the IP address. It should be initialized from the
> string. Later on, we can add functions to it."
> 
> [1] What in the world I have to do here?
> [1a] How to make class for the IP address? What does it mean?
> [2] "Initialization from the string" means something like passing
> arguments from the command line...?

Warning - oversimplification ahead!

With OOP, the idea is to make "Objects".  In this case you want to make an
object which represents an IP address. You will want (eventually) to have a
list of things that you want to do to an IP address object, like create
one, convert one to a name via the DNS, destroy one, pretend that CIDR
doesn't exist and get the netmask for one and so on. In order to do these
things you will need to have some data associated with the object. So all
we need to do is package these things together (the functions you want to
use with the object and the persistant data you need). This packaged
"thing" is called a "class". 

In python, you define a function called __init__ which makes the objects,
this is the initialization.

So you fire up a copy of python. and get a prompt. You then type

class ipaddress:
        def __init__(self,str):
                self.string_rep=str

and press return some more times until you get the >>>> prompt back.
This has defined a class called 'ipaddress', and made an initialization
routine for it. This routine takes 2 parameters, the first is traditionally
called 'self' and refers to the object itself. The second, named 'str', 
represents the string parameter. The action of the initialization routine
is to store this string into an internal variable (called string_rep).

You can now create an ipaddress object by typeing

an_ip=ipaddress("192.168.1.1")

and you can do things like

print an_ip.string_rep

Later on you will want to define other functions, and maybe add some more
data. For instance you may want to define __str__(self) so you can just
print out an object.

It is worth working through the python tutorial if you have not already done
so.

To make life more interesting, OOP uses different words, like "method" to
refer to one of the functions defined in the class. Above we created a
variable called 'an_ip' which is an "instance" of the ipaddress class, we
could make a second instance

another_ip=ipaddress("192.168.1.2")

Normally the __init__ method would do rather better error checking, so it
made sure that it was being given a sensible string value,

When you have more methods defined, you can use tham as

an_ip.netmask()

which would be written as
        netmask(an_ip)
in most languages which are not OOP languages.  Hope this very brief and
superficial introduction helps.






More information about the Python-list mailing list