1

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!

5
  • 2
    Too much work. Use a property. Commented Jun 22, 2018 at 21:38
  • 1
    Please edit this post to make it clearer. How did you print your first set of outputs. there are a lot of print() in there, with no output shown. And there is output where there's no print. Commented Jun 22, 2018 at 21:38
  • @Poppinyoshi Oops, I didn't copy the entire code. I'm an idiot. brb gonna fix it Commented Jun 22, 2018 at 21:46
  • @IgnacioVazquez-Abrams What do you mean? Commented Jun 22, 2018 at 21:50
  • @property def location(self): ` return (self.xcoord, self.ycoord)` Commented Jun 22, 2018 at 21:55

1 Answer 1

2

This is what properties are for.

Think of properties like methods that act like attributes. You need to calculate something on the fly when it's requested, but the thing it gets back isn't really an action it's more of a state. That's a property.

In this case you have:

class Matter():
    def __init__(self):
        self.x = 5
        self.y = 10

    @property
    def location(self):
        return (self.x, self.y)

Now you can use location as if it were an attribute, while it still works like a method.

m = Matter()
m.location  # (5, 10)
m.x, m.y = (20, 40)
m.location  # (20, 40)

However you CAN'T set via a property...

m.location = (40, 80)  # error

...unless you write a setter

# inside class Matter, after the code above
...
    @location.setter
    def location(self, newloc):
        self.x, self.y = newloc

Now you can, and it updates just like you say it should.

m.location = (40, 80)
m.x  # 40
m.y  # 80
m.location  # (40, 80)
Sign up to request clarification or add additional context in comments.

4 Comments

As an aside, this is what is meant when people say that Python assigns by value. When you did self.location = self.xcoord, self.ycoord, you were trying to say that "location is always the value of self.xcoord tupled with the value of self.ycoord." What your code actually did was "okay, so take the value stored at xcoord and the value stored at ycoord and store those values also at location.
That's great! exactly what I needed. if I were to set m.x=15 and m.y = 10 would that work the same way as the @property? in other words will that also update the m.location?
@Arximede yes, the property acts as a method which queries the current values at x and y.
@AdamSmith def __init__(self,x=0,y=0): self.x = x self.y = y , here I set default values for x and y. The behaviour seems to be same when I tested. Is there a difference?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.