How to change one or more attributes of a Python class/object based on their value









up vote
-1
down vote

favorite












I want to create a Python class which stores the name, surname and grades of a student and checks whether the grades are in the correct range or not (0-10).



Is there any way to change the attributes which are greater than 10 to 0 (not one by one)?



class Student:
def __init__(self, name, lastName, programming, algebra,
calculus, physics, writing):
self.name = name
self.lastName = lastName
self.programming = programming
self.algebra = algebra
self.calculus = calculus
self.physics = physics
self.writing = writing
def check(self):
if self.programming or self.algebra or self.calculus or self.physics or self.writing not in range(11):
#Change the value of the attributes which are > 10 to 0
#Is there any way to do it apart from this one?
#if self.programming not in range(11):
#self.programming = 0
#if self.algebra not in range(11):
#self.algebra = 0
...................









share|improve this question



















  • 3




    Yes. See: stackoverflow.com/questions/11637293/…. Alternatively, you could store all of those attributes in a dictionary student.marks and iterate over that
    – c2huc2hu
    Nov 10 at 17:56














up vote
-1
down vote

favorite












I want to create a Python class which stores the name, surname and grades of a student and checks whether the grades are in the correct range or not (0-10).



Is there any way to change the attributes which are greater than 10 to 0 (not one by one)?



class Student:
def __init__(self, name, lastName, programming, algebra,
calculus, physics, writing):
self.name = name
self.lastName = lastName
self.programming = programming
self.algebra = algebra
self.calculus = calculus
self.physics = physics
self.writing = writing
def check(self):
if self.programming or self.algebra or self.calculus or self.physics or self.writing not in range(11):
#Change the value of the attributes which are > 10 to 0
#Is there any way to do it apart from this one?
#if self.programming not in range(11):
#self.programming = 0
#if self.algebra not in range(11):
#self.algebra = 0
...................









share|improve this question



















  • 3




    Yes. See: stackoverflow.com/questions/11637293/…. Alternatively, you could store all of those attributes in a dictionary student.marks and iterate over that
    – c2huc2hu
    Nov 10 at 17:56












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I want to create a Python class which stores the name, surname and grades of a student and checks whether the grades are in the correct range or not (0-10).



Is there any way to change the attributes which are greater than 10 to 0 (not one by one)?



class Student:
def __init__(self, name, lastName, programming, algebra,
calculus, physics, writing):
self.name = name
self.lastName = lastName
self.programming = programming
self.algebra = algebra
self.calculus = calculus
self.physics = physics
self.writing = writing
def check(self):
if self.programming or self.algebra or self.calculus or self.physics or self.writing not in range(11):
#Change the value of the attributes which are > 10 to 0
#Is there any way to do it apart from this one?
#if self.programming not in range(11):
#self.programming = 0
#if self.algebra not in range(11):
#self.algebra = 0
...................









share|improve this question















I want to create a Python class which stores the name, surname and grades of a student and checks whether the grades are in the correct range or not (0-10).



Is there any way to change the attributes which are greater than 10 to 0 (not one by one)?



class Student:
def __init__(self, name, lastName, programming, algebra,
calculus, physics, writing):
self.name = name
self.lastName = lastName
self.programming = programming
self.algebra = algebra
self.calculus = calculus
self.physics = physics
self.writing = writing
def check(self):
if self.programming or self.algebra or self.calculus or self.physics or self.writing not in range(11):
#Change the value of the attributes which are > 10 to 0
#Is there any way to do it apart from this one?
#if self.programming not in range(11):
#self.programming = 0
#if self.algebra not in range(11):
#self.algebra = 0
...................






python python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 18:17









mpb

678810




678810










asked Nov 10 at 17:53









Sergio

21




21







  • 3




    Yes. See: stackoverflow.com/questions/11637293/…. Alternatively, you could store all of those attributes in a dictionary student.marks and iterate over that
    – c2huc2hu
    Nov 10 at 17:56












  • 3




    Yes. See: stackoverflow.com/questions/11637293/…. Alternatively, you could store all of those attributes in a dictionary student.marks and iterate over that
    – c2huc2hu
    Nov 10 at 17:56







3




3




Yes. See: stackoverflow.com/questions/11637293/…. Alternatively, you could store all of those attributes in a dictionary student.marks and iterate over that
– c2huc2hu
Nov 10 at 17:56




Yes. See: stackoverflow.com/questions/11637293/…. Alternatively, you could store all of those attributes in a dictionary student.marks and iterate over that
– c2huc2hu
Nov 10 at 17:56












1 Answer
1






active

oldest

votes

















up vote
1
down vote













You can use properties and setters and guard against wrong values inside the setters:



class Student:
def __init__(self, name, programming ):
self.name = name
self.programming = programming # using the setter so our business rule is applied

@property
def programming(self):
return self._programming

@programming.setter
def programming(self,n):
# apply the businessrule of n being in 1 to 10 else 0
self._programming = n if 0 < n < 11 else 0


studs = [ Student("Enya",20), Student("Eric",0), Student("Karl",5)]

for s in studs:
print(s.name, s.programming)


Output:



Enya 0
Eric 0
Karl 5


Read more about @property's at python.org property() and @property






share|improve this answer






















  • I just add, that when using getter and setter, attribute programming per se should be (conventionally) "private", i.e. self._programming = programming
    – Michal Polovka
    Nov 10 at 18:59










  • @MichalPolovka you cant name the backing variable of the getter the same - and I used the convention of one _ thats by convention signifies "privateness" - so I do not quite get what you mean.
    – Patrick Artner
    Nov 10 at 19:07










  • @MichalPolovka and the assignment in __init__(self) if purposefully using the property setter - so the buisinessrules are applied - you can use the private one directly but then you circumvent the setter buisiness rule implementation which would make it silly to use in the first place...
    – Patrick Artner
    Nov 10 at 19:11











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',
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
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241809%2fhow-to-change-one-or-more-attributes-of-a-python-class-object-based-on-their-val%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








up vote
1
down vote













You can use properties and setters and guard against wrong values inside the setters:



class Student:
def __init__(self, name, programming ):
self.name = name
self.programming = programming # using the setter so our business rule is applied

@property
def programming(self):
return self._programming

@programming.setter
def programming(self,n):
# apply the businessrule of n being in 1 to 10 else 0
self._programming = n if 0 < n < 11 else 0


studs = [ Student("Enya",20), Student("Eric",0), Student("Karl",5)]

for s in studs:
print(s.name, s.programming)


Output:



Enya 0
Eric 0
Karl 5


Read more about @property's at python.org property() and @property






share|improve this answer






















  • I just add, that when using getter and setter, attribute programming per se should be (conventionally) "private", i.e. self._programming = programming
    – Michal Polovka
    Nov 10 at 18:59










  • @MichalPolovka you cant name the backing variable of the getter the same - and I used the convention of one _ thats by convention signifies "privateness" - so I do not quite get what you mean.
    – Patrick Artner
    Nov 10 at 19:07










  • @MichalPolovka and the assignment in __init__(self) if purposefully using the property setter - so the buisinessrules are applied - you can use the private one directly but then you circumvent the setter buisiness rule implementation which would make it silly to use in the first place...
    – Patrick Artner
    Nov 10 at 19:11















up vote
1
down vote













You can use properties and setters and guard against wrong values inside the setters:



class Student:
def __init__(self, name, programming ):
self.name = name
self.programming = programming # using the setter so our business rule is applied

@property
def programming(self):
return self._programming

@programming.setter
def programming(self,n):
# apply the businessrule of n being in 1 to 10 else 0
self._programming = n if 0 < n < 11 else 0


studs = [ Student("Enya",20), Student("Eric",0), Student("Karl",5)]

for s in studs:
print(s.name, s.programming)


Output:



Enya 0
Eric 0
Karl 5


Read more about @property's at python.org property() and @property






share|improve this answer






















  • I just add, that when using getter and setter, attribute programming per se should be (conventionally) "private", i.e. self._programming = programming
    – Michal Polovka
    Nov 10 at 18:59










  • @MichalPolovka you cant name the backing variable of the getter the same - and I used the convention of one _ thats by convention signifies "privateness" - so I do not quite get what you mean.
    – Patrick Artner
    Nov 10 at 19:07










  • @MichalPolovka and the assignment in __init__(self) if purposefully using the property setter - so the buisinessrules are applied - you can use the private one directly but then you circumvent the setter buisiness rule implementation which would make it silly to use in the first place...
    – Patrick Artner
    Nov 10 at 19:11













up vote
1
down vote










up vote
1
down vote









You can use properties and setters and guard against wrong values inside the setters:



class Student:
def __init__(self, name, programming ):
self.name = name
self.programming = programming # using the setter so our business rule is applied

@property
def programming(self):
return self._programming

@programming.setter
def programming(self,n):
# apply the businessrule of n being in 1 to 10 else 0
self._programming = n if 0 < n < 11 else 0


studs = [ Student("Enya",20), Student("Eric",0), Student("Karl",5)]

for s in studs:
print(s.name, s.programming)


Output:



Enya 0
Eric 0
Karl 5


Read more about @property's at python.org property() and @property






share|improve this answer














You can use properties and setters and guard against wrong values inside the setters:



class Student:
def __init__(self, name, programming ):
self.name = name
self.programming = programming # using the setter so our business rule is applied

@property
def programming(self):
return self._programming

@programming.setter
def programming(self,n):
# apply the businessrule of n being in 1 to 10 else 0
self._programming = n if 0 < n < 11 else 0


studs = [ Student("Enya",20), Student("Eric",0), Student("Karl",5)]

for s in studs:
print(s.name, s.programming)


Output:



Enya 0
Eric 0
Karl 5


Read more about @property's at python.org property() and @property







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 10 at 19:09

























answered Nov 10 at 18:54









Patrick Artner

18.1k51940




18.1k51940











  • I just add, that when using getter and setter, attribute programming per se should be (conventionally) "private", i.e. self._programming = programming
    – Michal Polovka
    Nov 10 at 18:59










  • @MichalPolovka you cant name the backing variable of the getter the same - and I used the convention of one _ thats by convention signifies "privateness" - so I do not quite get what you mean.
    – Patrick Artner
    Nov 10 at 19:07










  • @MichalPolovka and the assignment in __init__(self) if purposefully using the property setter - so the buisinessrules are applied - you can use the private one directly but then you circumvent the setter buisiness rule implementation which would make it silly to use in the first place...
    – Patrick Artner
    Nov 10 at 19:11

















  • I just add, that when using getter and setter, attribute programming per se should be (conventionally) "private", i.e. self._programming = programming
    – Michal Polovka
    Nov 10 at 18:59










  • @MichalPolovka you cant name the backing variable of the getter the same - and I used the convention of one _ thats by convention signifies "privateness" - so I do not quite get what you mean.
    – Patrick Artner
    Nov 10 at 19:07










  • @MichalPolovka and the assignment in __init__(self) if purposefully using the property setter - so the buisinessrules are applied - you can use the private one directly but then you circumvent the setter buisiness rule implementation which would make it silly to use in the first place...
    – Patrick Artner
    Nov 10 at 19:11
















I just add, that when using getter and setter, attribute programming per se should be (conventionally) "private", i.e. self._programming = programming
– Michal Polovka
Nov 10 at 18:59




I just add, that when using getter and setter, attribute programming per se should be (conventionally) "private", i.e. self._programming = programming
– Michal Polovka
Nov 10 at 18:59












@MichalPolovka you cant name the backing variable of the getter the same - and I used the convention of one _ thats by convention signifies "privateness" - so I do not quite get what you mean.
– Patrick Artner
Nov 10 at 19:07




@MichalPolovka you cant name the backing variable of the getter the same - and I used the convention of one _ thats by convention signifies "privateness" - so I do not quite get what you mean.
– Patrick Artner
Nov 10 at 19:07












@MichalPolovka and the assignment in __init__(self) if purposefully using the property setter - so the buisinessrules are applied - you can use the private one directly but then you circumvent the setter buisiness rule implementation which would make it silly to use in the first place...
– Patrick Artner
Nov 10 at 19:11





@MichalPolovka and the assignment in __init__(self) if purposefully using the property setter - so the buisinessrules are applied - you can use the private one directly but then you circumvent the setter buisiness rule implementation which would make it silly to use in the first place...
– Patrick Artner
Nov 10 at 19:11


















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241809%2fhow-to-change-one-or-more-attributes-of-a-python-class-object-based-on-their-val%23new-answer', 'question_page');

);

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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto