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

In summary, the Student class has the following variables: first_name, last_name, email. The class also supports the following functions: set_id_number, set_email, set_dept.
  • #1
mathmari
Gold Member
MHB
5,049
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:
 
Technology news on Phys.org
  • #2
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.
 
  • #3
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:
 
  • #4
"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)
 
  • #5
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:
 
  • #6
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?
 
  • #7
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:
 
  • #8
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)
 
  • #9
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:
 
  • #10
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.
 

1. What is a class in Python?

A class in Python is a blueprint or template for creating objects. It defines the properties and behaviors that an object of that class will have.

2. What are methods in a Python class?

Methods are functions defined within a class that can be used to manipulate the data of an object of that class. They can also be used to perform specific actions on the object.

3. How do you define a method in a Python class?

To define a method in a Python class, you use the def keyword followed by the name of the method and parentheses. You can also specify parameters and a return statement if needed.

4. How do you call a method from a Python class?

To call a method from a Python class, you first need to create an instance of the class. Then, you use dot notation to access the method and provide any necessary arguments.

5. Can a Python class have multiple methods with the same name?

Yes, a Python class can have multiple methods with the same name as long as they have different parameters. This is known as method overloading and allows you to perform different actions depending on the arguments passed to the method.

Similar threads

  • Programming and Computer Science
2
Replies
43
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
13
Views
4K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
10
Views
10K
  • Programming and Computer Science
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
23
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top