Xivilai
Xivilai

Reputation: 2521

Python - Default values not working

I have edited using some of your comments below. I am just looking to have a default value for a parameter where one is not provided. I still do not get any values when I run the following two lines. There are now default values for the variables that will be used if the class is called without data

newPatient = patient()

print(newPatient.firstname)


import os
from sikuli import *

class patient():

    firstname = ""
    middlename = ""
    Surname = ""
    title = ""
    ppsno = ""
    gender = ""
    testID = ""
    birthDate = ""
    address1 = ""
    address2 = ""
    address3 = ""
    address4 = ""
    town = ""
    county = ""
    country = ""
    email = ""
    mobile = ""

#Default Constructor for the class patient

#Use this to create a patient with more detailed information

    def __init__(
        self,
        testID,
        firstname = 'Sample',
        middlename = 'Mary',
        surname = 'Patient',
        gender = 'Female',
        birthDate = '03091959',
        title = 'Mrs',            
        ppsno = '7445213P',
        address1 = '100',
        address2 = 'Green Glade',
        address3 = 'Pleasant Way',
        address4 = 'Ballybehy',
        town = 'Abbyfeale',
        county = 'Limerick',
        country = 'Ireland',
        email = '[email protected]',
        mobile = '0870563229'):

        self.testID = testID

        self.firstname = firstname
        self.middlename = middlename
        self.surname = surname
        self.gender = gender
        self.birthDate = birthDate
        self.title = title        
        self.ppsno = ppsno
        self.address1 = address1
        self.address2 = address2
        self.address3 = address3
        self.address4 = address4
        self.town = town
        self.county = county
        self.country = country
        self.email = email
        self.mobile = mobile

    def getfirstname(self):
         return self.firstname

    class schemeDetails():
        cardNumber = ""
        scheme = ""
        cardNumber = ""
        month = ""
        year = ""
        setSchemeAsDefault = ""

    def __init__(
        self,
        scheme = None,
        cardNumber = None,
        month = None,
        year = None,
        setSchemeAsDefault = None ):

        if (scheme is None):
            scheme = "GM"
            self.scheme = scheme
        if (cardNumber is None):
            month = "1231456A"
            self.cardNumber = cardNumber
        if (month is None):
            month = "September"
            self.month = month
        if (year is None):
            year = "2015"
            self.year = year
        if (setSchemeAsDefault is None):
            setSchemeAsDefault = "true"
            self.setSchemeAsDefault = setSchemeAsDefault

    def getStuff(self):
        return self.stuff

#Inner class for creating basic dispenses
    class basicDispense():
        drug = ""
        packSize = ""
        dosageSystem = ""
        scheme = ""
        #Constructor for the class basicDispense
        def __init__(
            self,
            drug = None,
            packSize = None,
            dosageSystem = None,
            scheme = None):

            if (drug is None):
                drug = "ABBOTT THIN LANCET TYPE C GMS"
                self.drug = drug
            if (packSize is None):
                packSize = "28"
                self.packSize = packSize
            if (dosageSystem is None):
                dosageSystem = "MD"
                self.dosageSystem = dosageSystem
            if (scheme is None):
                scheme = "GM"
                self.scheme = scheme

            def getStuff(self):
                return self.stuff

        #Inner class of basicDispenses for printing Labels
        #Constructor for the class Labels
        class labels():
            def __init__(
                self,
                testID,
                printBagLabel = None,
                printDrugLabel = None):

                self.testID = testID
                if (printBagLabel is None):
                    printBagLabel = "false"
                    self.drug = drug
                if (printDrugLabel is None):
                    printDrugLabel = "false"
                    self.printDrugLabel = printDrugLabel

Upvotes: 0

Views: 2352

Answers (3)

Martin Konecny
Martin Konecny

Reputation: 59581

Your class attributes should be set if you aren't passing in an argument for that specific parameter. Show us some proof that this isn't the case - you are probably printing the class variable instead of the instance variable

p = patient()
p.firstname # should print the default firstname
patient.firstname # should print an empty string

On a side note, why don't you initialize the following way (it's much more compact, and easy to read):

def __init__(
    self,
    testID,
    firstname = "Sample",
    middlename = "Patient",
    surname = "Mary",
    gender = "Female",
    birthDate = "03091959",
    title = "Mrs",            
    ppsno = "7445213P",
    ...
):

EDIT:

OK it makes sense what else is wrong now - your indentation. Calling newPatient = patient() would fail if your indentation was correct (because you have one mandatory parameter, testID). Make sure you indent your entire __init__ function to the right by 4 spaces.

Upvotes: 3

RickyA
RickyA

Reputation: 16029

I dont get your use of the default values. They are obviously test data and should therefore not be in the class. What about something like this to construct a class with test data:

class Patient():
    def __init__(
                self,
                test_id,
                firstname,
                middlename):

        self.test_id = test_id
        self.firstname = firstname
        self.middlename = middlename

test_data = {"test_id":123, "firstname":"foo", "middlename":"bar"}
p = Patient(**test_data)

print(p.test_id)
print(p.firstname)
print(p.middlename)

Gives

>>>123
>>>foo
>>>bar

Upvotes: 1

gkusner
gkusner

Reputation: 1244

The whole class should look like this - (using the tips before)

import os
from sikuli import *

class patient():
    def __init__(
        self,
        testID,
        firstname = 'Sample',
        middlename = 'Mary',
        surname = 'Patient',
        gender = 'Female',
        birthDate = '03091959',
        title = 'Mrs',            
        ppsno = '7445213P',
        address1 = '100',
        address2 = 'Green Glade',
        address3 = 'Pleasant Way',
        address4 = 'Ballybehy',
        town = 'Abbyfeale',
        county = 'Limerick',
        country = 'Ireland',
        email = '[email protected]',
        mobile = '0870563229'):

        self.testID = testID

        self.firstname = firstname
        self.middlename = middlename
        self.surname = surname
        self.gender = gender
        self.birthDate = birthDate
        self.title = title        
        self.ppsno = ppsno
        self.address1 = address1
        self.address2 = address2
        self.address3 = address3
        self.address4 = address4
        self.town = town
        self.county = county
        self.country = country
        self.email = email
        self.mobile = mobile

Upvotes: 1

Related Questions