0
What am I missing?
I'm learning about encapsulation and need to use a protected and private variable. When I run it, it tells me "protected() is undefined" I know it's probably the smallest thing, but can't quite figure it out, any help would be appreciated. class User: _name = "" __id = 0 def Info(self, _name, __id): self._name = _name self.__id = __id def protected(self,User): self._name = "Peaches" def Private(self): self.__id = 1234 def getPrivate(self): self.__id = 0 print(self.__id) def setPrivate(self, id): self.__id = id obj = Protected() obj.getPrivate() obj.setPrivate(1234) obj.getPrivate()
2 Antworten
+ 1
protected() is a method, not a function. So you have to create an instance of the User class first and then call its method.
0
I see now, thank you so much!