-1

I receive the error "NameError: name '_Activity__self' is not defined". I used self as a parameter in everything and for the members so I'm not sure what I'm missing. -- I'm trying to have Activity be the parent class and WakeUp be derived from Activity.

class Activity:

        def __init__ (self, d, t):
                __self.date = d
                __self.time = t

        def getDate(self):
                return self.date

        def getTime(self):
                return self.time


class WakeUp (Activity):

        def __init__ (self, d, t):
                Activity.__init__(self, d, t)
                __self.activityType = "WakeUp"

        def getType(self):
                return self.activityType
mak95
  • 21
  • 5

2 Answers2

1

You are using __self instead of self.
This should work
.

class Activity:

        def __init__ (self, d, t):
                self.date = d
                self.time = t

        def getDate(self):
                return self.date

        def getTime(self):
                return self.time


class WakeUp (Activity):

        def __init__ (self, d, t):
                Activity.__init__(self, d, t)
                self.activityType = "WakeUp"

        def getType(self):
                return self.activityType
  • but to make it a private don't I have to put __ beforehand? – mak95 Nov 27 '21 at 18:59
  • @mak95 that would be `self.__time`. I would recommend spending more time googling, because the private variable thing was there in the first search result. – Extorc Productions Nov 27 '21 at 19:02
  • @mak95 https://stackoverflow.com/questions/1641219/does-python-have-private-variables-in-classes – Extorc Productions Nov 27 '21 at 19:02
  • okay thank you, I was following my professor's slides on python classes and he put the __ before self so I assumed that he was correct... learned not to trust my professor today! – mak95 Nov 27 '21 at 19:05
  • @mak95 The first programing lesson ever should be how to use google. Because it has more information than Stack Overflow does. Moreover, the private variable solution itself is a SO question. Have a good day. – Extorc Productions Nov 27 '21 at 19:06
1

__self would work if __self was in the tuple, eg (__self, d, t). Note that the first parameter in the 'init' tuple determines the 'self' name for the class. Convention recommends `self' but in fact it can be anything you want. It is the position in the parameters tht is important.

Check out 'What is self' in https://docs.python.org/3/faq/programming.html?highlight=self#what-is-self

InhirCode
  • 338
  • 1
  • 4
  • 5