Based on what I tested the in the following code, when I define the self.location attribute in the class Matter(), and when I'm trying to assign the value to the instance it doesn't work.
class Matter():
"""docstring for Matter"""
def __init__(self):
self.xcoord = 0
self.ycoord = 0
self.location = (self.xcoord, self.ycoord)
main = Matter()
#before changing values
print(main.xcoord, main.ycoord)
#changing values
main.xcoord = 5
main.ycoord = 10
print(main.xcoord, main.ycoord)
print(main.location)
output:
- 0 0
- 5 10
- (0, 0)
self.location did not change in this case. but when I do this:
main = Matter()
# before changinv the values
print(main.xcoord, main.ycoord)
# changing the values
main.xcoord = 5
main.ycoord = 10
print(main.xcoord, main.ycoord)
print(main.location)
class Matter():
"""docstring for Matter"""
def __init__(self):
self.xcoord = 0
self.ycoord = 0
def set_location(self):
self.location = (self.xcoord, self.ycoord)
main = Matter()
print(main.xcoord, main.ycoord)
main.xcoord = 5
main.ycoord = 10
Matter.set_location(main)
print(main.xcoord, main.ycoord)
print(main.location)
output:
- 0 0
- 5 10
- (5, 10)
bonus question: Any attribute and method that I can create in the class, can be used and modified by using different functions that aren't in the class? I might have confused between attribute vs instance there but if someone can clarify I would be grateful!
Thank you!
print()in there, with no output shown. And there is output where there's no print.@propertydef location(self):` return (self.xcoord, self.ycoord)`