Playing With Python

Python (source: http://xkcd.com/353/)

It’s so true. I have been doing small programs to learn Python and I am loving it. My Python editor of choice is pyscripter. It gave me everything I was looking for in an IDE:
  • File Tabs
  • Auto-Complete
  • Line Numbers
  • Complete Control of the IDE settings
    • Colors
    • Hotkeys
    • Window Location
  • Open Source
Whenever I tackle a language I am unfamiliar with I start with the same test project: Create a Person class, create an Employee class that inherits from the Person class, and make a tester that creates both, adds them to a list and then iterates through them displaying the results.
This is a simple task, but allows me to view the same concept in multiple languages. It also helps me learn what is the same and what is different.
Code after the break.
Here is what I came up with for Python (this is mostly to test my WordPress Syntax Highlighter):
Person.py
class Person:
    # Constructor
    def __init__(self, n, a):
        # Private Variables
        self.__name = n
        self.__age = a

    # One way to do properties
    @property
    def name(self): return self.__name
    @name.setter
    def name(self, value): self.__name = value
    @name.deleter
    def name(self): del self.__name

    # Another way to do properties
    def __get__age(self): return self.__age
    def __set__age(self, value): self.__age = value
    def __del__age(self): del self.__age
    age = property(__get__age, __set__age, __del__age, "Age of the person")

    def ToString(self):
        # Print Person Information
        print("Name: %s" % self.name)
        print("Age: %d" % self.age)
Employee.py
from Person import Person

# Employee inherits from Person
class Employee(Person):
    # Constructor
    def __init__(self, name, age, job):
        # Call Parent's constructor
        super(Employee, self).__init__(name, age)
        # Do my own setting
        self.__job = job

    def ToString(self):
        # Display Employee Information
        super().ToString()
        print("Workplace: %s" % self.__job)

    # Employee specific function
    def Work(self):
        print("Going to work.")
PersonTester.py
from Person import Person
from Employee import Employee

# If the program is run as a main program
if __name__ == '__main__':
    # Create a person
    p = Person("Harrold", 62)
    # Create an Employee
    e = Employee("Nancy", 21, "Burger Flipper")

    # Make an array of people
    parray = [p, e]

    # Iterate through them and display the results
    for m in parray:
        m.ToString()
        print("")
With the results of:
>>>
Name: Harrold
Age: 62

Name: Nancy
Age: 21
Workplace: Burger Flipper

So far Python is making me happy :)

Edit @ 2:39pm

Reading more, found the default “ToString” of Python is __str__, adjusted accordingly:

Person.py

class Person:
    # Constructor
    def __init__(self, n, a):
        # Private Variables
        self.__name = n
        self.__age = a

    @property
    def name(self): return self.__name
    @name.setter
    def name(self, value): self.__name = value
    @name.deleter
    def name(self): del self.__name

    @property
    def age(self): return self.__age
    @age.setter
    def age(self, value): self.__age = value
    @age.deleter
    def age(self): del self.__age

    def __str__(self):
        # Print Person Information
        return "Name: {0} {1}Age: {2}".format(self.name, "\n",self.age)

Employee.py

from Person import Person

# Employee inherits from Person
class Employee(Person):
    # Constructor
    def __init__(self, name, age, job):
        # Call Parent's constructor
        super(Employee, self).__init__(name, age)
        # Do my own setting
        self.__job = job

    @property
    def job(self): return self.__job
    @job.setter
    def job(self, value): self.__job = value
    @job.deleter
    def job(self): del self.__job

    def __str__(self):
        # Display Employee Information
        return "{0}{1}Workplace: {2}".format(Person.__str__(self), "\n", self.job)

PersonTester.py

from Person import *
from Employee import *

# If the program is run as a main program
if __name__ == '__main__':
    # Create a person
    p = Person("Harrold", 62)
    # Create an Employee
    e = Employee("Nancy", 21, "Burger Flipper")

    # Make an array of people
    parray = [p, e]

    # Iterate through them and display the results
    for m in parray:
        print(m)
        print("")

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>