Good practice when defining classes

This is the place to post any code that you want to share with the community. Only completed scripts should be posted here.

Good practice when defining classes

Postby Natalie » Fri May 23, 2014 3:57 pm

Hi all,

I'm relatively new to Python and OO so wanted to rewrite a simple Matlab code. I'm comfortable with the definitions of classes but I'm finding it a little difficult to know what the best way to define them. Most tutorials I've found give examples using animals so then it's kind of obvious. I'm not sure if I'm just using classes like I would functions. The code below compares two possible population growth models for calculating number of occupants from the floor area in a property. I've used an 'estimation_type' class and then defined the two methods as its children. I thought this would keep things nicer but in hindsight there are no methods that relate to this class so might just be unnecessary. I think it might have been better to define a general estimation class with methods representing the two options. I'd appreciate any advice on this because, like I say, I'm new to this so I'm keen to learn the best practice from the offset.

Code: Select all
# Rewriting simple Matlab code in Python OO framework

import math
import array
import matplotlib

class estimation_type:
    number_of_occupants = []
    floor_area = []

class bounded_exponential_growth(estimation_type):
    Bound = 0.0 # Maximum number of people in a property

    def calculate_num_occupants(self):
        for i in range(len(self.floor_area)):
            k = (-1/13.9)*math.log(1-(1/self.Bound))
            self.number_of_occupants = self.number_of_occupants + [self.Bound*(1-math.exp(-k*self.floor_area[i]))]

class logistic_growth(estimation_type):
    Bound = 0.0 # Maximum number of people in a property
   
    def calculate_num_occupants(self):
        for i in range(len(self.floor_area)):
            a = math.exp(13.9*0.05)*(self.Bound - 1)
            self.number_of_occupants = self.number_of_occupants + [self.Bound/(1+(a*math.exp(-0.05*self.floor_area[i])))]

# Estimate number of occupants
estimation_exp = bounded_exponential_growth()
estimation_log = logistic_growth()

estimation_exp.Bound = estimation_log.Bound = 10.0
estimation_exp.floor_area = estimation_log.floor_area = range(10,210,10)

estimation_exp.calculate_num_occupants()
num_occ_exp = estimation_exp.number_of_occupants
estimation_log.calculate_num_occupants()
num_occ_log = estimation_log.number_of_occupants

# Plot values

import numpy as np
import pylab as pl

plot1 = pl.plot(estimation_exp.floor_area, num_occ_exp)
plot2 = pl.plot(estimation_log.floor_area, num_occ_log)

pl.xlabel("Floor area ($m^2$)")
pl.ylabel("Number of occupants")
pl.title("Estimating number of occupants from floor area of property")

pl.legend([plot1[0], plot2[0]], ["Bounded exponential growth", "Logistic growth"],"best")
pl.show()
Last edited by Mekire on Fri May 23, 2014 4:00 pm, edited 1 time in total.
Reason: First post lock.
Natalie
 
Posts: 3
Joined: Fri May 23, 2014 3:47 pm

Re: Good practice when defining classes

Postby stranac » Fri May 23, 2014 4:18 pm

I would just use two functions, taking Bound and floor_area as arguments and returning number_of_occupants.
In general, if your class only has a single method, it should probably be a function instead.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm

Re: Good practice when defining classes

Postby Natalie » Fri May 23, 2014 4:26 pm

Thank you stranac. What if I defined an 'occupant' class instead as below? Is there any advantage of doing this over using functions? I'm just finding it difficult to understand when OO would be preferable to using functions. Perhaps if I had a property class with certain variables (e.g. floor area, number of windows, etc...) and I wanted to do a number of different calculations using these variables an OO framework would be better? For example I might want to estimate the number of occupants, the heat loss of the building etc...

Code: Select all
class occupants:
    number_of_occupants_exp = []
    number_of_occupants_log = []
    floor_area = []
    Bound = 0.0 # Maximum number of people in a property

    def exp_growth(self):
        for i in range(len(self.floor_area)):
            k = (-1/13.9)*math.log(1-(1/self.Bound))
            self.number_of_occupants_exp = self.number_of_occupants_exp + [self.Bound*(1-math.exp(-k*self.floor_area[i]))]

   
    def log_growth(self):
        for i in range(len(self.floor_area)):
            a = math.exp(13.9*0.05)*(self.Bound - 1)
            self.number_of_occupants_log = self.number_of_occupants_log + [self.Bound/(1+(a*math.exp(-0.05*self.floor_area[i])))]


Natalie
 
Posts: 3
Joined: Fri May 23, 2014 3:47 pm

Re: Good practice when defining classes

Postby stranac » Fri May 23, 2014 5:02 pm

Natalie wrote:What if I defined an 'occupant' class instead as below? Is there any advantage of doing this over using functions?

Not really.

Natalie wrote: Perhaps if I had a property class with certain variables (e.g. floor area, number of windows, etc...) and I wanted to do a number of different calculations using these variables an OO framework would be better? For example I might want to estimate the number of occupants, the heat loss of the building etc...

Yes, that would probably make more sense.

You basically use classes when you have some data and multiple functions that need to share that data.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm

Re: Good practice when defining classes

Postby Larz60+ » Thu May 29, 2014 3:00 pm

Hello,

I also quite new to python, though have been programming for many years. You might want to check out the following discussion on when to and not to use classes, and what should be contained within each:

https://mail.python.org/pipermail/tutor/2003-June/023512.html

Larz60+
Larz60+
 
Posts: 294
Joined: Thu Apr 03, 2014 4:06 pm

Re: Good practice when defining classes

Postby Natalie » Tue Jun 03, 2014 1:02 pm

That's great. Thank you both for your help.
Natalie
 
Posts: 3
Joined: Fri May 23, 2014 3:47 pm


Return to Completed Scripts

Who is online

Users browsing this forum: No registered users and 1 guest