How to declare an attribute in Python without a value?


In Python, as well as in several other languages, there is a value that means "no value". In Python, that value with no value is None. So the following shows how None is used −

class Student:
   StudentName = None
   RollNumber = None

Those are like instance variables though, and not class variables, so we may as well write −

class Student(object):
    def __init__(self):
        self.StudentName = None
        self.RollNumber = None

We can see how Python assigns the None value implicitly in the code below −

def h():
k = h() # k now has the value of None
Published on 12-Jan-2018 12:38:31