I am trying to create a function that calls an attribute determined by what argument is passed.
class room:
def __init__(self, length, bredth, depth):
self.length = length
self.bredth = bredth
self.depth = depth
def displaymeasurement(self, side):
print(self.side)
kitchen = room(10, 20, 15)
room.displaymeasurement("depth")
This is an abstraction of the code I'm using as that's too convoluted. I have striven to match it to the code in question, and it does produce the same error message.
Traceback (most recent call last):
File "/home/townsend/Documents/PycharmProjects/untitled2/Test/inplicitreference.py", line 13, in <module>
room.displaymeasurement("depth")
File "/home/townsend/Documents/PycharmProjects/untitled2/Test/inplicitreference.py", line 8, in displaymeasurement
print(self.side)
AttributeError: 'shape' object has no attribute 'side'
What syntax am I missing to communicate to the computer to replace side
with the entered argument depth
and process from there.
I have spent a couple of days searching but I can't seem to find an attempt at a similar construction. Perhaps it's because I'm using incorrect terminology. I am very new to this.
I don't expect this method to work but I thought it was the best way to illustrate. I have tried several different methods.
I am aware of a series of if
checks as a solution but I'm convinced there's an simpler and more expandable solution.
def displaymeasurement(self, side):
if side == "length":
print(self.length)
if side == "bredth":
print(self.bredth)
if side == "depth":
print(self.depth)