How do we properly set and get instance variables in a class?

  • Thread starter Thread starter mathmari
  • Start date Start date
  • Tags Tags
    Class Python
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
9 replies · 2K views
mathmari
Gold Member
MHB
Messages
4,984
Reaction score
7
Hey! :giggle:

Construct a class named Student which when initializing its objects has the following variables :
- first_name (string)
- last_name (string)
- email (string)

The class should also support the following functions implemented as functions (methods):
- set_id_number: set a registration number (string)
- set_email: set an email (string)
- set_dept: set the department to which the student belongs
- get_id_number: returns the student registration number
- get_email: returns the student's email
- get_dept: returns the department to which the student belongs I have done the following :

Code:
class Student : 
    def __init__(self, first_name, last_name, email):
        self.first_name = first_name
        self.last_name = last_name 
        self.email = email 

    def set_id_number(self) : 
        
    def set_email(self, email) : 
        self.email = email 
    
    def set_dept(self) : 
        
    def get_id_number(self) : 
        
    def get_email(self) : 
        print ("The email of the student is : " self.email) 
        
    def get_dept(self) :

Is the initialization correct?
Are the methods set and get email correct?
How do construct the methods for id and department? Which information do we use for that?

:unsure:
 
Physics news on Phys.org
Hi, the init stuff is correct. Your code won't run without adding "pass" to the blank methods. That's a good way of writing placeholders until you write it out in full. This is based on your code and will test an example instance of the class.

Code:
class Student :
    def __init__(self, first_name, last_name, email):
        self.first_name = first_name
        self.last_name = last_name
        self.email = email

    def set_id_number(self):
        pass
        
    def set_email(self, email) :
        self.email = email
    
    def set_dept(self):
        pass
        
    def get_id_number(self):
        pass
        
    def get_email(self) :
        pass
        
    def get_dept(self):
        pass

student = Student("John", "Doe", "johndoe@gmail.com")

How could you test if set_email() is working? Try it on the student object above.
 
Jameson said:
How could you test if set_email() is working? Try it on the student object above.

We write :
Code:
student = Student("John", "Doe", "johndoe@gmail.com")
student.set_email(student.email)

and respectively also for the other methods, right? :unsure:So do we let inside the methods "pass" or do we write a code to do the specific task? :unsure:
 
"pass" just let's this be valid syntax. Try running the code without it and it should break. It's a placeholder until you write the actual methods. pass = "do nothing but still exist".

Here's how you can set the email using the method you wrote.

Code:
student = Student("John", "Doe", "johndoe@gmail.com")
print(student.email)
student.email = "NEWjohndoe@yahoo.com"
print(student.email)
 
Jameson said:
"pass" just let's this be valid syntax. Try running the code without it and it should break. It's a placeholder until you write the actual methods. pass = "do nothing but still exist".

Here's how you can set the email using the method you wrote.

Code:
student = Student("John", "Doe", "johndoe@gmail.com")
print(student.email)
student.email = "NEWjohndoe@yahoo.com"
print(student.email)

I see!

But for example set_id_number do we maybe count the students and give them the correspinding number as anid? Or how can we set an id? And what about set_dept ? We don't have any information about the department before, how canwe set that then? :unsure:
 
Ok thinking about this a little more it looks like they want to have a set/get pattern for various attributes. I think this is an example of what they are looking for.
Code:
class Student :
    def __init__(self, first_name, last_name, email):
        self.first_name = first_name
        self.last_name = last_name
        self.email = email

    def set_id_number(self, id_number):
        self.id_number = id_number

    def get_id_number(self):
        return self.id_number
        
    def set_email(self, email) :
        pass

    def get_email(self) :
        pass
    
    def set_dept(self):
        pass
    
    def get_dept(self):
        pass

student = Student("John", "Doe", "johndoe@gmail.com")
student.set_id_number(12345)
print(student.get_id_number())

What would this look like for others?
 
Jameson said:
Ok thinking about this a little more it looks like they want to have a set/get pattern for various attributes. I think this is an example of what they are looking for.
Code:
class Student :
    def __init__(self, first_name, last_name, email):
        self.first_name = first_name
        self.last_name = last_name
        self.email = email

    def set_id_number(self, id_number):
        self.id_number = id_number

    def get_id_number(self):
        return self.id_number
       
    def set_email(self, email) :
        pass

    def get_email(self) :
        pass
   
    def set_dept(self):
        pass
   
    def get_dept(self):
        pass

student = Student("John", "Doe", "johndoe@gmail.com")
student.set_id_number(12345)
print(student.get_id_number())

What would this look like for others?

Ah I got it! So we write :
Code:
class Student :
    def __init__(self, first_name, last_name, email):
        self.first_name = first_name
        self.last_name = last_name
        self.email = email

    def set_id_number(self, id_number):
        self.id_number = id_number

    def get_id_number(self):
        return self.id_number
        
    def set_email(self, email) :
        self.email = email 

    def get_email(self) :
        return self.email 
    
    def set_dept(self, dept):
        self.dept = dept 
    
    def get_dept(self):
        return self.dept 

student = Student("John", "Doe", "johndoe@gmail.com")
student.set_id_number(12345)
print(student.get_id_number()) 
student.set_email("newjohndoe@gmail.com")
print(student.get_email()) 
student.set_dept("Mathematics")
print(student.get_dept())

Is that correct ? :unsure:

Constrructing an object of the class means to set and get the information as we did in the main part of the code? :unsure:
 
That looks good! There are a couple of topics here:
  • In the __init__ method, these are parameters that must be supplied to initialize the class object. So whatever you put here must be given when the class instance (student) is created.
  • The set/get methods are kind of conventions instead of just setting these properties like this:
    • student.email = "newjohndoe@gmail.com"
This would both set this property and retrieve it, but it's bad practice for reasons I can't quite remember and using a set/get pair is considered best practice.​
  • When you have properties not part of the __init___ method you use the get/set methods to work with them so you have the syntax Class.set_property(property) like student.set_id_number(12345)
 
Jameson said:
That looks good! There are a couple of topics here:
  • In the __init__ method, these are parameters that must be supplied to initialize the class object. So whatever you put here must be given when the class instance (student) is created.
  • The set/get methods are kind of conventions instead of just setting these properties like this:
    • student.email = "newjohndoe@gmail.com"
This would both set this property and retrieve it, but it's bad practice for reasons I can't quite remember and using a set/get pair is considered best practice.​
  • When you have properties not part of the __init___ method you use the get/set methods to work with them so you have the syntax Class.set_property(property) like student.set_id_number(12345)
So you mean that the methods that set an information that we have already in the class is just to alter the given information and if we set a new information it is that we give this for the speciic instant, right? :unsure:
 
Class instance variables can be set when class is initialized or later on. If required when initialized it will be part of the __init__ method. id_number is an example of an instance variable that isn't set during initialization though.

The way we set/get these variables is separated into two methods by convention. There are some more complex reasons why I think, but for now I would just file this info away as good practice.