By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,293 Members | 1,397 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,293 IT Pros & Developers. It's quick & easy.

Need help with OOP

P: n/a
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...?

Of course, I don't expect you to do the job for me, but I would sure
appreciate help. I know this is easy, but as stated before, I don't
know how to handle objects... yet :)

I informed my 'mentor' that the training on OOP subject has begun and
I started looking for good literature.

In the mean time, I'm trying to buy some time, and I was hoping that
your suggestions could help me.

Thanks for the help, and if I was unclear at some point, just shoot :)

Ciao,
Sinisa
P.S. Can somebody please explain how can I.... how to avoid all these
"I"'s from being used as often as I'm doing it? :( It really goes on
my nerves as I feel like illiterate idiot :(

P.P.S. Speaking of being illiterate, no, English is not my native
language. Just in case someone wanders... :o)
Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Fri, 7 Nov 2003 05:36:57 +0100, sinisam <X.***************@mail.ru>
wrote:
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?
One feature you will often see in OOP is the idea of 'wrapping' more
primitive concepts into objects and then giving those objects more
functionality as a means of convenience. For example, while it is
possible to trate a phone number as a simple string such as

phoneNumber = '102.555.1234'

You can also wrapper the simple string in a class and then give it
extra functionality

phoneNumber = PhoneNumber()
phoneNumber._number = "102.555.1234"
pre = phoneNumber.prefix()

the function "prefix()" is defined by the PhoneNumber class as an
easier way of dealing with the phone number. To create this wrapper
class in Python is easy

class PhoneNumber:
def __init__(self):
self._number = ''

def prefix (self):
return self._number[4:7]

So the question being asked is to write a similar wrapper class that
will do the same for IP addresses

[2] "Initialization from the string" means something like passing
arguments from the command line...?

No, it means that when you create the object, you will provide some
information that will set the default state of the new object

person = Person ("O'Connor", "James")

this is done by providing paramaters for your initialization function
for the class

class Person:

def __init__(self, lastName, firstName):
self._lastName = lastName
self._firstName = firstName

This sets the lastName and firstName of the person to whatever you
tell it to be when you create the object
Of course, I don't expect you to do the job for me, but I would sure
appreciate help. I know this is easy, but as stated before, I don't
know how to handle objects... yet :)


The above explanation and examples are sparse, but should be
sufficient to move you in the right direction to solve your problem

Take care,
Jay
Jul 18 '05 #2

P: n/a
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.
Jul 18 '05 #3

P: n/a
On Fri, 7 Nov 2003 05:36:57 +0100, sinisam
<X.***************@mail.ru> wrote:
Object oriented programming is not a new term to me, but ...
I did my share of programming several years ago...
Don't worry if OOP takes aq while to sink in, thats pretty
common, especially if you've been trained in procedural styule
programming - you have to learn to change your approach to
problem solving.

It might be worth going through some of the beginners
tutors on the Python web site, most of which - including
mine! ;-) - have a section on OOP.
"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?
Turn an IP address into an OOP object.
[1a] How to make class for the IP address? What does it mean?
Read the tutorials, they will take you step by step through the
comcepts.
[2] "Initialization from the string" means something like passing
arguments from the command line...?
A little bit, but as part of the initialisation call of your
object. Python contains lots of objects. For example files are
objects.

You create a file object by passing the filename as a parameter:

f = file("foo.txt")

f is now a file object that you can maniplulate. You are being
asked to provide a similar facility for IP addresses:

ip = IPaddress("123.234,321.34")
I started looking for good literature.
Try the beginners tutors and also for the theoretical
understanding visit www.cetus-links.org which is a huge
OOP portal site.
P.P.S. Speaking of being illiterate, no, English is not my native
language. Just in case someone wanders... :o)


You're doing just fine on that front. Better than some who do ave
English as their native tongue! :-)

Alan G.

Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld
Jul 18 '05 #4

P: n/a
On Fri, 07 Nov 2003 06:47:20 GMT, Icarus Sparry
<us****@icarus.freeuk.com> wrote:

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


However, using str as (even a local) variable name is not that good an
idea, since it's the name of the built-in type representing strings,
which could not be used in this __init__ method because of that. An
example which does just that could be:

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

to convert whatever parameter gets passed to a string. Using built-in
names for variables is a habit that shouldn't be started ;-)

--
Christopher
Jul 18 '05 #5

P: n/a
On Fri, 7 Nov 2003 05:36:57 +0100, sinisam <X.***************@mail.ru>
wrote:

....
"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...?

Of course, I don't expect you to do the job for me, but I would sure

....
This won't directly help with learning OOP, but be sure to read the
Python module documentation for the socket module, and the
higher-level protocol support (e.g., httplib, ftplib, smtplib, etc).
I'm no socket expert, and didn't see anything in it that was an
actual "ip class", but there are a lot of methods for manipulating IP
addresses in string and binary form.
--dang
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.