Need help with OOP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sinisam

    Need help with OOP

    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] "Initializa tion 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)
  • Jay O'Connor

    #2
    Re: Need help with OOP

    On Fri, 7 Nov 2003 05:36:57 +0100, sinisam <X.3.M__antispa m__@mail.ru>
    wrote:
    [color=blue]
    >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?[/color]

    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._nu mber = "102.555.12 34"
    pre = phoneNumber.pre fix()

    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

    [color=blue]
    >[2] "Initializa tion from the string" means something like passing
    >arguments from the command line...?[/color]


    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
    [color=blue]
    >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 :)[/color]

    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

    Comment

    • Icarus Sparry

      #3
      Re: Need help with OOP

      sinisam wrote:
      [color=blue]
      > 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...[/color]
      [color=blue]
      > 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] "Initializa tion from the string" means something like passing
      > arguments from the command line...?[/color]

      Warning - oversimplificat ion 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,s tr):
      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_re p

      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=ipad dress("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.


      Comment

      • Alan Gauld

        #4
        Re: Need help with OOP

        On Fri, 7 Nov 2003 05:36:57 +0100, sinisam
        <X.3.M__antispa m__@mail.ru> wrote:[color=blue]
        > Object oriented programming is not a new term to me, but ...
        > I did my share of programming several years ago...[/color]

        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.
        [color=blue]
        > "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?[/color]

        Turn an IP address into an OOP object.
        [color=blue]
        > [1a] How to make class for the IP address? What does it mean?[/color]

        Read the tutorials, they will take you step by step through the
        comcepts.
        [color=blue]
        > [2] "Initializa tion from the string" means something like passing
        > arguments from the command line...?[/color]

        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")
        [color=blue]
        > I started looking for good literature.[/color]

        Try the beginners tutors and also for the theoretical
        understanding visit www.cetus-links.org which is a huge
        OOP portal site.
        [color=blue]
        > P.P.S. Speaking of being illiterate, no, English is not my native
        > language. Just in case someone wanders... :o)[/color]

        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

        Comment

        • Christopher Koppler

          #5
          Re: Need help with OOP

          On Fri, 07 Nov 2003 06:47:20 GMT, Icarus Sparry
          <usenet@icarus. freeuk.com> wrote:
          [color=blue]
          >
          >class ipaddress:
          > def __init__(self,s tr):
          > self.string_rep =str[/color]

          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_str ing = 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

          Comment

          • Dang Griffith

            #6
            Re: Need help with OOP

            On Fri, 7 Nov 2003 05:36:57 +0100, sinisam <X.3.M__antispa m__@mail.ru>
            wrote:

            ....[color=blue]
            >"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] "Initializa tion 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[/color]
            ....
            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

            Comment

            Working...