-4

I am learning about classes and objects in python. I encountered a problem when I tried to create a class attribute whose value can be changed using an instance of that class. Lets assume create a class Student for students who go to the same school:

class Students:
    school = "Elimu"
    def __init__(self, name = "", grade= 1):
        self.name = name
        self.grade = grade

student_1 = Students("Imara", 5)
student_2 = Students("Jabali", 7)

I want to be able to change the class attribute using the class name and using the instance of the class i.e class_name.class_attribute = new_value class_instance.class_attribute = new_value

For further illustration(following the previous code):

Students.school
Students.school = "Ganjoni"
Students.school
student_1.school = "Vikwale"
Students.school
student_1.school
student_2.school

My desired Output:

Elimu
Ganjoni
Vikwale
Vikwale
Vikwale

Actual Output:

Elimu
Gajoni
Ganjoni
Vikwale
Ganjoni

Amani
  • 57
  • 4
  • `self.class = class` should throw a syntax error. Doesn't it? – Robin De Schepper Jan 08 '23 at 11:53
  • Please post some working cod in the example. You are mixing a lot class names and the variables: Students vs. Student, student_1 vs. student1. Also `class` is a keyword in python, so your `def __init__(self, name = "", class= 1):` will not work. – Marcel Preda Jan 08 '23 at 11:54
  • `student_1.school = "Vikwale"` changes the instance variable not the class variable – Maurice Meyer Jan 08 '23 at 12:14
  • @MarcelPreda, did a correction on my question and have tried to be more clear on my intentions. Please help me with the above question – Amani Jan 08 '23 at 13:02

2 Answers2

1

I don't imagine it was intentional but you can't use class as a valid parameter name since its a keyword.

Something that should be noted here is the fact that python isn't exactly strict to its static attributes (unlike most other programming languages) as in it really doesn't care if you change it (Unless you haven't previously set it in which case its going to directly inherit its default value)

Students.school = "Some other school"

Is just going to make it so that the default value of all classes to "Some other school". Meaning you can't change the value of all other classes from a single instance and change all the values of classes with a set attribute from the main class. So:

Students.school = "a"
studentOne = Students()
studentOne.school = "b" # <- Changes to the main class will no longer affect studentOne's school
Students.school = "c"   # <- doesn't change the value of studentOnes school
studentTwo = Students() # <- but makes it so now any instance with an unset school is going to have a default school value of "c"

Same goes for:

studentOne.school = "d" # <- doesn't change studentTwo's school value or the main classes school value
  • This is not correct: changing the class attribute will change it for new instances, but also for existing instances _unless they have been assigned a specific value before_ – gimix Jan 08 '23 at 12:31
  • @gimix With a little bit of testing I was able to figure out that, yes you are in fact correct. This was my mistake, apologies – Dimitur Karabuyukov Jan 08 '23 at 12:35
0

Am going to assume that you are posting this question cause python gave you some kind of error (its important to add the error you get to your questions). If that is so, the reason for the error is because you are trying to use the word "class" as an attribute, this is not allowed because, "class" is a keyword, the following is a list of keywords for python : ( i took the following data from https://www.w3schools.com/python/python_ref_keywords.asp ) enter image description here

(image source https://www.resourcenote.info/2020/02/python-overview.html)

So any word that is a keyword can not be used as a variable name, function name, class name, class attribute name, function input parameter name etc. Those names are sacred.

class Students:
    school = "Elimu"
    def __init__(self, name = "",  ̶c̶l̶a̶s̶s̶ = 1):
        self.name = name
        self. ̶c̶l̶a̶s̶s̶ =  ̶c̶l̶a̶s̶s̶

student_1 = Students("Imara", 5)
student_2 = Students("Jabali", 7)

so in the above code, i have strikethroug the wrong uses of "class", replace it with something else or add something to it "sh_class" will do, or anything it just cant be "class".