0

I'm trying to run a class named person. The error is name 'person' is not defined. How can I solve this problem?

class person:
    person.count = 0
    def __init__(self,gender,location,DOB):
        # this is constructor method
        self.__gender = gender
        self.__location = location
        self.__DOB = DOB
        # to make the variable inaccessible from out of the class 
        # we have to prefix it with at least two underscores
        print(person.__self)
        print(person.__DOB)
        person.count += 1
    def getname(self):
        #this is access method
        return person.__self
    def getDOB(self):
        return person.__DOB
    def _del_(self):
        print('deleted')
martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    This is actually very simple, though. Just change `person.count = 0` to `count = 0`. It's already in the `person` scope. – glibdud Jun 10 '19 at 14:34
  • Also, `person.__self` is going to fail, because you never assign `__self`. Everywhere you want to access the attribute of a specific person, you should be accessing the attribute of the instance, `self`, not the class, `person`. – Patrick Haugh Jun 10 '19 at 14:34
  • Note that `_del_` should be `__del__`, that is a double underscore. However, as a destructor it is unreliable. – cdarke Jun 10 '19 at 14:44
  • >>> krishna = person('male','pune','26/05/1996') Traceback (most recent call last): File "", line 1, in krishna = person('male','pune','26/05/1996') TypeError: 'module' object is not callable – krishna_soni Jun 10 '19 at 14:51
  • i edited as per your instruction but, another error is shown when i try to create an object – krishna_soni Jun 10 '19 at 14:51

1 Answers1

1

First of all, put the line person.count = 0 inside __init__. Next, change every person to self (unless it's the class definition).

Jacob
  • 225
  • 1
  • 9
  • also `__self` is not defined anywhere. You might want to define it in `__init__ – Omri Attiya Jun 10 '19 at 14:39
  • 2
    I think `count` is deliberately a class variable, not an instance variable. It appears to be a way to track the number of instances created. Adding it to `__init__()` will cause it to reset every time an instance is created. – glibdud Jun 10 '19 at 14:40