python call function in class 1

python call function in class

#------------------------------------
#CLASS
#------------------------------------
class Student:
  def __init__(self):
    self.name = None
    
  def set_name(self, word):
    self.name = word
    return self.get_name()
    
  def get_name(self):
    return self.name
  
#------------------------------------
# USAGE:
#------------------------------------

a = Student()
print(a.set_name("Hello"))

Here is what the above code is Doing:
1. We create a class called Student.
2. We create a function called __init__. This is a special function that is called when we create a new instance of the class.
3. We create a variable called name and set it to None.
4. We create a function called set_name that takes a parameter called word.
5. We set the name variable to the value of word.
6. We create a function called get_name that returns the value of name.
7. We create a new instance of the class called a.
8. We call the set_name function and pass it the string “Hello”.
9. We print the value returned by set_name.

Similar Posts