Same GET-Property for multiple Attributes
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I recently heared about properties, and that you can assign functions to the getter or setter of a attribute within a class. So you can do value Checks or different things.
I want to ask if it's possible to assign the same function to the getter of multiple attributes. To do that the function needs a dynamic return, so that the value X is returned if X was called, and Y if Y was called.
I could do a own getter for every Attribute, but in my case are 20+ Attributes which need to call that Function, so it would be nicer to have the dynamic call.
Testing with __getattributes__
was a total disaster, because I always got it looped endlessly, so either is wasn't possible that way or I just made mistakes.
Is that possible? Something like that:
class example():
def __init__(self):
self.__x = 1
self.__y = 2
def do_something(self):
#Do something which is needed by multiple attributes
#without changes
attribute_name = '__' + attribute_name
return self.__dict__[attribute_name]
x = property(fget=do_something)
y = property(fget=do_something)
...
EDIT 1:
My full Code where I could need dynamic property Getter.
https://repl.it/@TobiasWagner/TV-DB-Django
python dynamic properties attributes
add a comment |
I recently heared about properties, and that you can assign functions to the getter or setter of a attribute within a class. So you can do value Checks or different things.
I want to ask if it's possible to assign the same function to the getter of multiple attributes. To do that the function needs a dynamic return, so that the value X is returned if X was called, and Y if Y was called.
I could do a own getter for every Attribute, but in my case are 20+ Attributes which need to call that Function, so it would be nicer to have the dynamic call.
Testing with __getattributes__
was a total disaster, because I always got it looped endlessly, so either is wasn't possible that way or I just made mistakes.
Is that possible? Something like that:
class example():
def __init__(self):
self.__x = 1
self.__y = 2
def do_something(self):
#Do something which is needed by multiple attributes
#without changes
attribute_name = '__' + attribute_name
return self.__dict__[attribute_name]
x = property(fget=do_something)
y = property(fget=do_something)
...
EDIT 1:
My full Code where I could need dynamic property Getter.
https://repl.it/@TobiasWagner/TV-DB-Django
python dynamic properties attributes
Sure, you can implement the descriptor protocol and just provide the backing attribute name, e.g.x = magic_property('__x')
. Note that leading double underscores probably isn't what you want, though: stackoverflow.com/q/7456807/3001761.
– jonrsharpe
Nov 16 '18 at 11:53
You might also find codereview.stackexchange.com/q/98892/32391 interesting.
– jonrsharpe
Nov 16 '18 at 11:59
@jonrsharpe Thanks for the quick response, I kinda at a loss here. I don't quite understand whats going on at your post. I tried playing around with__getattribute__(self, name)
but it failed because I can't use functions in there. And if I want to work with properties I need to transfer the name of the calling attribute to the new getter. Here is my Code, maybe it will help. [Class TBDB.py Row 180] REPL.IT
– Tkay
Nov 16 '18 at 14:31
Please edit to give a Minimal, Complete, and Verifiable example
– jonrsharpe
Nov 16 '18 at 14:36
add a comment |
I recently heared about properties, and that you can assign functions to the getter or setter of a attribute within a class. So you can do value Checks or different things.
I want to ask if it's possible to assign the same function to the getter of multiple attributes. To do that the function needs a dynamic return, so that the value X is returned if X was called, and Y if Y was called.
I could do a own getter for every Attribute, but in my case are 20+ Attributes which need to call that Function, so it would be nicer to have the dynamic call.
Testing with __getattributes__
was a total disaster, because I always got it looped endlessly, so either is wasn't possible that way or I just made mistakes.
Is that possible? Something like that:
class example():
def __init__(self):
self.__x = 1
self.__y = 2
def do_something(self):
#Do something which is needed by multiple attributes
#without changes
attribute_name = '__' + attribute_name
return self.__dict__[attribute_name]
x = property(fget=do_something)
y = property(fget=do_something)
...
EDIT 1:
My full Code where I could need dynamic property Getter.
https://repl.it/@TobiasWagner/TV-DB-Django
python dynamic properties attributes
I recently heared about properties, and that you can assign functions to the getter or setter of a attribute within a class. So you can do value Checks or different things.
I want to ask if it's possible to assign the same function to the getter of multiple attributes. To do that the function needs a dynamic return, so that the value X is returned if X was called, and Y if Y was called.
I could do a own getter for every Attribute, but in my case are 20+ Attributes which need to call that Function, so it would be nicer to have the dynamic call.
Testing with __getattributes__
was a total disaster, because I always got it looped endlessly, so either is wasn't possible that way or I just made mistakes.
Is that possible? Something like that:
class example():
def __init__(self):
self.__x = 1
self.__y = 2
def do_something(self):
#Do something which is needed by multiple attributes
#without changes
attribute_name = '__' + attribute_name
return self.__dict__[attribute_name]
x = property(fget=do_something)
y = property(fget=do_something)
...
EDIT 1:
My full Code where I could need dynamic property Getter.
https://repl.it/@TobiasWagner/TV-DB-Django
python dynamic properties attributes
python dynamic properties attributes
edited Nov 16 '18 at 14:34
Tkay
asked Nov 16 '18 at 11:49
TkayTkay
398
398
Sure, you can implement the descriptor protocol and just provide the backing attribute name, e.g.x = magic_property('__x')
. Note that leading double underscores probably isn't what you want, though: stackoverflow.com/q/7456807/3001761.
– jonrsharpe
Nov 16 '18 at 11:53
You might also find codereview.stackexchange.com/q/98892/32391 interesting.
– jonrsharpe
Nov 16 '18 at 11:59
@jonrsharpe Thanks for the quick response, I kinda at a loss here. I don't quite understand whats going on at your post. I tried playing around with__getattribute__(self, name)
but it failed because I can't use functions in there. And if I want to work with properties I need to transfer the name of the calling attribute to the new getter. Here is my Code, maybe it will help. [Class TBDB.py Row 180] REPL.IT
– Tkay
Nov 16 '18 at 14:31
Please edit to give a Minimal, Complete, and Verifiable example
– jonrsharpe
Nov 16 '18 at 14:36
add a comment |
Sure, you can implement the descriptor protocol and just provide the backing attribute name, e.g.x = magic_property('__x')
. Note that leading double underscores probably isn't what you want, though: stackoverflow.com/q/7456807/3001761.
– jonrsharpe
Nov 16 '18 at 11:53
You might also find codereview.stackexchange.com/q/98892/32391 interesting.
– jonrsharpe
Nov 16 '18 at 11:59
@jonrsharpe Thanks for the quick response, I kinda at a loss here. I don't quite understand whats going on at your post. I tried playing around with__getattribute__(self, name)
but it failed because I can't use functions in there. And if I want to work with properties I need to transfer the name of the calling attribute to the new getter. Here is my Code, maybe it will help. [Class TBDB.py Row 180] REPL.IT
– Tkay
Nov 16 '18 at 14:31
Please edit to give a Minimal, Complete, and Verifiable example
– jonrsharpe
Nov 16 '18 at 14:36
Sure, you can implement the descriptor protocol and just provide the backing attribute name, e.g.
x = magic_property('__x')
. Note that leading double underscores probably isn't what you want, though: stackoverflow.com/q/7456807/3001761.– jonrsharpe
Nov 16 '18 at 11:53
Sure, you can implement the descriptor protocol and just provide the backing attribute name, e.g.
x = magic_property('__x')
. Note that leading double underscores probably isn't what you want, though: stackoverflow.com/q/7456807/3001761.– jonrsharpe
Nov 16 '18 at 11:53
You might also find codereview.stackexchange.com/q/98892/32391 interesting.
– jonrsharpe
Nov 16 '18 at 11:59
You might also find codereview.stackexchange.com/q/98892/32391 interesting.
– jonrsharpe
Nov 16 '18 at 11:59
@jonrsharpe Thanks for the quick response, I kinda at a loss here. I don't quite understand whats going on at your post. I tried playing around with
__getattribute__(self, name)
but it failed because I can't use functions in there. And if I want to work with properties I need to transfer the name of the calling attribute to the new getter. Here is my Code, maybe it will help. [Class TBDB.py Row 180] REPL.IT– Tkay
Nov 16 '18 at 14:31
@jonrsharpe Thanks for the quick response, I kinda at a loss here. I don't quite understand whats going on at your post. I tried playing around with
__getattribute__(self, name)
but it failed because I can't use functions in there. And if I want to work with properties I need to transfer the name of the calling attribute to the new getter. Here is my Code, maybe it will help. [Class TBDB.py Row 180] REPL.IT– Tkay
Nov 16 '18 at 14:31
Please edit to give a Minimal, Complete, and Verifiable example
– jonrsharpe
Nov 16 '18 at 14:36
Please edit to give a Minimal, Complete, and Verifiable example
– jonrsharpe
Nov 16 '18 at 14:36
add a comment |
0
active
oldest
votes
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%2f53337288%2fsame-get-property-for-multiple-attributes%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53337288%2fsame-get-property-for-multiple-attributes%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
Sure, you can implement the descriptor protocol and just provide the backing attribute name, e.g.
x = magic_property('__x')
. Note that leading double underscores probably isn't what you want, though: stackoverflow.com/q/7456807/3001761.– jonrsharpe
Nov 16 '18 at 11:53
You might also find codereview.stackexchange.com/q/98892/32391 interesting.
– jonrsharpe
Nov 16 '18 at 11:59
@jonrsharpe Thanks for the quick response, I kinda at a loss here. I don't quite understand whats going on at your post. I tried playing around with
__getattribute__(self, name)
but it failed because I can't use functions in there. And if I want to work with properties I need to transfer the name of the calling attribute to the new getter. Here is my Code, maybe it will help. [Class TBDB.py Row 180] REPL.IT– Tkay
Nov 16 '18 at 14:31
Please edit to give a Minimal, Complete, and Verifiable example
– jonrsharpe
Nov 16 '18 at 14:36