我已经使用下面的一些评论进行了编辑。我只是希望有一个参数的默认值,其中没有提供一个参数。当我运行以下两行时,我仍然没有得到任何值。现在,如果在没有数据的情况下调用类
,将使用的变量有默认值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 = 'supertest69@freewebmail.ie',
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
答案 0 :(得分:2)
如果您没有传递该特定参数的参数,则应设置您的类属性。向我们展示一些证据,证明情况并非如此 - 您可能正在打印类变量而不是实例变量
p = patient()
p.firstname # should print the default firstname
patient.firstname # should print an empty string
另一方面,为什么不按照以下方式进行初始化(它更紧凑,易于阅读):
def __init__(
self,
testID,
firstname = "Sample",
middlename = "Patient",
surname = "Mary",
gender = "Female",
birthDate = "03091959",
title = "Mrs",
ppsno = "7445213P",
...
):
修改强>
确定现在还有什么问题 - 你的缩进是有道理的。如果缩进正确,则调用newPatient = patient()
将失败(因为您有一个必需参数testID
)。确保将整个__init__
函数向右缩进4个空格。
答案 1 :(得分:1)
全班应该是这样的 - (使用之前的提示)
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 = 'supertest69@freewebmail.ie',
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
答案 2 :(得分:1)
我没有使用默认值。它们显然是测试数据,因此不应该在课堂上。用这样的东西构建一个包含测试数据的类:
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)
给出
>>>123
>>>foo
>>>bar