Computer >> Computer tutorials >  >> Programming >> Python

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():
    pass
k = h() # k now has the value of None