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
...................
python python-3.x
add a comment |
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
...................
python python-3.x
3
Yes. See: stackoverflow.com/questions/11637293/…. Alternatively, you could store all of those attributes in a dictionarystudent.marks
and iterate over that
– c2huc2hu
Nov 10 at 17:56
add a comment |
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
...................
python python-3.x
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
python python-3.x
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 dictionarystudent.marks
and iterate over that
– c2huc2hu
Nov 10 at 17:56
add a comment |
3
Yes. See: stackoverflow.com/questions/11637293/…. Alternatively, you could store all of those attributes in a dictionarystudent.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
add a comment |
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
I just add, that when using getter and setter, attributeprogramming
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
add a comment |
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
I just add, that when using getter and setter, attributeprogramming
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
add a comment |
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
I just add, that when using getter and setter, attributeprogramming
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
add a comment |
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
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
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, attributeprogramming
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
add a comment |
I just add, that when using getter and setter, attributeprogramming
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
add a comment |
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%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
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
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