Property for changing an object-attribute in an object
I need to call a method, when a attribute of an object within an object is changed. So I went with properties but without success.
I used properties to check if the object within the object is changing. But that only calls the setter if I assign a new or existing object of that type and not when I change attribute within.
class Position:
def __init__(self):
self.x = 0
self.y = 0
class Player:
def __init__(self, x, y):
self.__position = Position()
self.__position.x = x
self.__position.y = y
@property
def position(self):
return self.__position
@position.setter
def position(self, value):
print('Position changed')
self.__position = value
player = Player(5, 10)
player.position = Position() # Setter gets called
player.position.x = 10 # Setter doesn't get called
Currently that example prints "Position changed" once in line player.position = Position()
. But I need to call the setter, when X or Y is changed.
Thanks in advance!
python class properties
add a comment |
I need to call a method, when a attribute of an object within an object is changed. So I went with properties but without success.
I used properties to check if the object within the object is changing. But that only calls the setter if I assign a new or existing object of that type and not when I change attribute within.
class Position:
def __init__(self):
self.x = 0
self.y = 0
class Player:
def __init__(self, x, y):
self.__position = Position()
self.__position.x = x
self.__position.y = y
@property
def position(self):
return self.__position
@position.setter
def position(self, value):
print('Position changed')
self.__position = value
player = Player(5, 10)
player.position = Position() # Setter gets called
player.position.x = 10 # Setter doesn't get called
Currently that example prints "Position changed" once in line player.position = Position()
. But I need to call the setter, when X or Y is changed.
Thanks in advance!
python class properties
2
When you do player.position.x you are not setting player.position, you are just changing values in the position object. player.position is still the same Position instance it was. player.position = Position(10,10) would probably work as expected.
– Matthew Page
Jan 19 at 21:16
@MatthewPage and there is no way to check if X or Y is changed? Without assigning a new Position?
– Tkay
Jan 19 at 21:43
sorry, not that far into Python yet, but in theory yes there is. If Player extended the Position class would the Position class know about it's parent, again I'm not sure about Python OO. The Position class needs to say 'Tell what ever created me that I've changed' , usually by calling message method in the parent..?
– Matthew Page
Jan 19 at 21:52
add a comment |
I need to call a method, when a attribute of an object within an object is changed. So I went with properties but without success.
I used properties to check if the object within the object is changing. But that only calls the setter if I assign a new or existing object of that type and not when I change attribute within.
class Position:
def __init__(self):
self.x = 0
self.y = 0
class Player:
def __init__(self, x, y):
self.__position = Position()
self.__position.x = x
self.__position.y = y
@property
def position(self):
return self.__position
@position.setter
def position(self, value):
print('Position changed')
self.__position = value
player = Player(5, 10)
player.position = Position() # Setter gets called
player.position.x = 10 # Setter doesn't get called
Currently that example prints "Position changed" once in line player.position = Position()
. But I need to call the setter, when X or Y is changed.
Thanks in advance!
python class properties
I need to call a method, when a attribute of an object within an object is changed. So I went with properties but without success.
I used properties to check if the object within the object is changing. But that only calls the setter if I assign a new or existing object of that type and not when I change attribute within.
class Position:
def __init__(self):
self.x = 0
self.y = 0
class Player:
def __init__(self, x, y):
self.__position = Position()
self.__position.x = x
self.__position.y = y
@property
def position(self):
return self.__position
@position.setter
def position(self, value):
print('Position changed')
self.__position = value
player = Player(5, 10)
player.position = Position() # Setter gets called
player.position.x = 10 # Setter doesn't get called
Currently that example prints "Position changed" once in line player.position = Position()
. But I need to call the setter, when X or Y is changed.
Thanks in advance!
python class properties
python class properties
asked Jan 19 at 21:10
TkayTkay
398
398
2
When you do player.position.x you are not setting player.position, you are just changing values in the position object. player.position is still the same Position instance it was. player.position = Position(10,10) would probably work as expected.
– Matthew Page
Jan 19 at 21:16
@MatthewPage and there is no way to check if X or Y is changed? Without assigning a new Position?
– Tkay
Jan 19 at 21:43
sorry, not that far into Python yet, but in theory yes there is. If Player extended the Position class would the Position class know about it's parent, again I'm not sure about Python OO. The Position class needs to say 'Tell what ever created me that I've changed' , usually by calling message method in the parent..?
– Matthew Page
Jan 19 at 21:52
add a comment |
2
When you do player.position.x you are not setting player.position, you are just changing values in the position object. player.position is still the same Position instance it was. player.position = Position(10,10) would probably work as expected.
– Matthew Page
Jan 19 at 21:16
@MatthewPage and there is no way to check if X or Y is changed? Without assigning a new Position?
– Tkay
Jan 19 at 21:43
sorry, not that far into Python yet, but in theory yes there is. If Player extended the Position class would the Position class know about it's parent, again I'm not sure about Python OO. The Position class needs to say 'Tell what ever created me that I've changed' , usually by calling message method in the parent..?
– Matthew Page
Jan 19 at 21:52
2
2
When you do player.position.x you are not setting player.position, you are just changing values in the position object. player.position is still the same Position instance it was. player.position = Position(10,10) would probably work as expected.
– Matthew Page
Jan 19 at 21:16
When you do player.position.x you are not setting player.position, you are just changing values in the position object. player.position is still the same Position instance it was. player.position = Position(10,10) would probably work as expected.
– Matthew Page
Jan 19 at 21:16
@MatthewPage and there is no way to check if X or Y is changed? Without assigning a new Position?
– Tkay
Jan 19 at 21:43
@MatthewPage and there is no way to check if X or Y is changed? Without assigning a new Position?
– Tkay
Jan 19 at 21:43
sorry, not that far into Python yet, but in theory yes there is. If Player extended the Position class would the Position class know about it's parent, again I'm not sure about Python OO. The Position class needs to say 'Tell what ever created me that I've changed' , usually by calling message method in the parent..?
– Matthew Page
Jan 19 at 21:52
sorry, not that far into Python yet, but in theory yes there is. If Player extended the Position class would the Position class know about it's parent, again I'm not sure about Python OO. The Position class needs to say 'Tell what ever created me that I've changed' , usually by calling message method in the parent..?
– Matthew Page
Jan 19 at 21:52
add a comment |
1 Answer
1
active
oldest
votes
Question: I need to call the setter, when X or Y is changed.
If you want to get informed, if x
or y
get changed, use x.setter
and y.setter
.
For example:
@x.setter
def x(self, value):
print('Position X changed')
self.__position.x = value
Usage:
player.x = 10
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54271414%2fproperty-for-changing-an-object-attribute-in-an-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Question: I need to call the setter, when X or Y is changed.
If you want to get informed, if x
or y
get changed, use x.setter
and y.setter
.
For example:
@x.setter
def x(self, value):
print('Position X changed')
self.__position.x = value
Usage:
player.x = 10
add a comment |
Question: I need to call the setter, when X or Y is changed.
If you want to get informed, if x
or y
get changed, use x.setter
and y.setter
.
For example:
@x.setter
def x(self, value):
print('Position X changed')
self.__position.x = value
Usage:
player.x = 10
add a comment |
Question: I need to call the setter, when X or Y is changed.
If you want to get informed, if x
or y
get changed, use x.setter
and y.setter
.
For example:
@x.setter
def x(self, value):
print('Position X changed')
self.__position.x = value
Usage:
player.x = 10
Question: I need to call the setter, when X or Y is changed.
If you want to get informed, if x
or y
get changed, use x.setter
and y.setter
.
For example:
@x.setter
def x(self, value):
print('Position X changed')
self.__position.x = value
Usage:
player.x = 10
answered Jan 20 at 10:49
stovflstovfl
7,70731031
7,70731031
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54271414%2fproperty-for-changing-an-object-attribute-in-an-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
When you do player.position.x you are not setting player.position, you are just changing values in the position object. player.position is still the same Position instance it was. player.position = Position(10,10) would probably work as expected.
– Matthew Page
Jan 19 at 21:16
@MatthewPage and there is no way to check if X or Y is changed? Without assigning a new Position?
– Tkay
Jan 19 at 21:43
sorry, not that far into Python yet, but in theory yes there is. If Player extended the Position class would the Position class know about it's parent, again I'm not sure about Python OO. The Position class needs to say 'Tell what ever created me that I've changed' , usually by calling message method in the parent..?
– Matthew Page
Jan 19 at 21:52