[Tutor] Beginner Question

Andy McKenzie amckenzie4 at gmail.com
Tue Oct 22 19:26:08 CEST 2013


On Tue, Oct 22, 2013 at 10:25 AM, Sven Hennig <shennig93 at googlemail.com>wrote:

> Hello, I would like to learn a programming language and have decided to
> use Python. I have some programming experience and doing well in Python.
> What really causes me problems is OOP.
> I'm just dont get it... I'm missing a really Practical example. In every
> book I've read are the examples of such Class Dog and the function is bark
> . Has anyone an OOP example for me as it is really used in real code, so
> I can better understand the concept? I do not know why this is so hard for
> me.
>
> Greetings
> Sven
>
>
The actual code I have is in PHP, so it's not exactly useful to post here,
but I can give you an outline of a class I used and found useful.  The
class was created to help me manipulate network subnets in an interface to
handle DHCP and DNS on a server.

class subnet:

   def __init__(cidr):
      # Do some stuff with the CIDR notation of the subnet (1.2.3.0/24syntax)

  def netmask:
     # Calculate the netmask and return it (ie, 255.255.255.0)

   def first_ip:
      # Calculate and return the first IP in the range.

   def last_ip:
      # Calculate and return the last IP in the range

   def number_of_addresses:
      # Calculate the number of usable addresses in the range and return
that value

The benefit to me was that I could create an instance of the subnet object
for a group (say, "lab_1"), and then pull out various information.  Calling
"lab_1.first_ip()" returns the first possible IP address.  That was a lot
more readable and a lot more concise than something like "first_ip(
1.2.3.0/24)".


I hope this helps!

Andy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131022/f6dd19dd/attachment.html>


More information about the Tutor mailing list