Python 3.9.5
Mac OS
Hi All,
I am working on an assignment and could really use some help. It involves two different files (Customer.py and main.py). The instructions I don't understand are Steps 2 and 3.
This is what I have tried: Every variable in customer.py uses the _variable notation (2 underscores) which I think protects others from assigning something to them. It seems to me that to retrieve the right value the main program would use myCustomer.getEmail() instead of myCustomer.Email but they don't. I believe the answer lies in using the name mangling prevention technique with double underscores for the variables. I have played with this and I'm not getting anywhere.
THESE ARE THE STEPS I DON'T UNDERSTAND:
2) Modify the Class File to take advantage of Name Mangling for the Email Attribute so that the property cannot be modified directly by a consumer of the class.
3) add at least 1 additional attribute to the class and make sure your new attribute takes advantage of Name Mangling.
THIS IS THE CODE (there are two files copies and pasted, customer.py and main.py.
Customer.py file
Mac OS
Hi All,
I am working on an assignment and could really use some help. It involves two different files (Customer.py and main.py). The instructions I don't understand are Steps 2 and 3.
This is what I have tried: Every variable in customer.py uses the _variable notation (2 underscores) which I think protects others from assigning something to them. It seems to me that to retrieve the right value the main program would use myCustomer.getEmail() instead of myCustomer.Email but they don't. I believe the answer lies in using the name mangling prevention technique with double underscores for the variables. I have played with this and I'm not getting anywhere.
THESE ARE THE STEPS I DON'T UNDERSTAND:
2) Modify the Class File to take advantage of Name Mangling for the Email Attribute so that the property cannot be modified directly by a consumer of the class.
3) add at least 1 additional attribute to the class and make sure your new attribute takes advantage of Name Mangling.
THIS IS THE CODE (there are two files copies and pasted, customer.py and main.py.
Customer.py file
class Customer: __Name="" __DateOfBirth="1/1/1900" __AccountNumber = "Unassigned" #note no name mangling on Email attribute (Accessible from outside) Email = "[email protected]" #constructor def __init__(self,Name,DOB,AccountNum,Email): self.__Name = Name self.__DateOfBirth=DOB self.__AccountNumber = AccountNum self.Email = Email def getName(self): return self.__Name def getDOB(self): return self.__DateOfBirth def getAccountNumber(self): return self.__AccountNumber def getEmail(self): return self.Email def setName(self,newName): self.__Name = newName def setDOB(self,newDOB): self.__DateOfBirth = newDOB def setAccountNumber(self,newAcctNum): self.__AccountNumber = newAcctNum def setEmail(self,newEmail): self.Email = newEmail def __del__(self): print("Object instance successfully removed")main.py file
from Customer import Customer def Main(): myCustomer = Customer("Bob","12/12/1982","1090-332","[email protected]") print(myCustomer.getName() + " " + str(myCustomer.getDOB()) + " " + str(myCustomer.getAccountNumber()) + " " + myCustomer.getEmail()) myCustomer.Email = "THIS IS A BAD EMAIL ADDRESS" #WORKS print(myCustomer.getName() + " " + myCustomer.getDOB() + " " + myCustomer.getAccountNumber() + " " + myCustomer.Email) myCustomer.AccountNumber = "BAD Account Number" #Doesnt Work...OR DOES IT ? print(myCustomer.getAccountNumber()) #OLD Account Number still in tact print(myCustomer.AccountNumber) #python can be confusing in this regard. BUT our internal data IS still safe Except for the Email that does NOT use Name Mangling print(myCustomer.getName() + " " + myCustomer.getDOB() + " " + myCustomer.getAccountNumber() + " " + myCustomer.Email) Main()
Larz60+ write Jun-10-2021, 01:54 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode tags on future posts.
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode tags on future posts.