Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

added 115 characters in body
Source Link
abarnert
  • 368k
  • 54
  • 627
  • 692

... which, you'll notice, is Peter DeGlopper's answer, which, as I said at the start, is probably what you want. :)

... which, you'll notice, is Peter DeGlopper's answer, which, as I said at the start, is probably what you want. :)

Source Link
abarnert
  • 368k
  • 54
  • 627
  • 692

If you really want private variables in Python... then you don't want private variables, and should read Peter DeGlopper's answer.

If you still really, really want private variables in Python... well, you can't have them. But you can have "cooperatively private" variables—variables that nobody will find unless they go looking for them, and that won't clutter the screen when you introspect things in the interpreter, and so on, and, most importantly, that Python programmers know, by convention, that they aren't supposed to touch. All you have to do is start the name with an underscore.


However, your code isn't creating member variables at all, for a number of reasons.

First, global does not declare or define a variable; all it does is tell Python, "when you see this variable later, don't use the normal rules to figure out if it's local or global, always use the global copy". You still have to assign a value to the variable somewhere; otherwise, you'll get a NameError.

Next, variables that you assign in the class definition are class members—similar to Java's static members, although not identical. Each class member is shared by all instances of the class. That's not what you want here; each Node is supposed to have its own separate val and next, not share one with all other Nodes, right?

Normal instance member variables are always accessed through dot syntax—as self.foo from inside the class's methods, or as spam.foo from outside.

So, where do you declare those? You don't. Python doesn't declare anything. You can add new members to an object at any time. The usual way to create a standard set of instance members is in the __init__ method:

class Node(object):
 def __init__(self):
 self._next = None
 self._val = None
 def setNext(self, aNext):
 self._next = aNext
 def getNext(self):
 return self._next
 def setVal(self, aVal):
 self._val = aVal
 def getVal(self):
 return self._val

But really, you can just let the setters create them. That way, you'll catch the error if someone calls getNext without having called setNext first (which is, I assume, illegal).

class Node(object):
 def setNext(self, aNext):
 self._next = aNext
 def getNext(self):
 return self._next
 def setVal(self, aVal):
 self._val = aVal
 def getVal(self):
 return self._val

Or, alternatively, force the user to initialize the object with valid values at construction time:

 def __init__(self, next, val):
 self._next = next
 self._val = val

Again, there's no good reason to use setters and getters in the first place in Python.

So, the simplest implementation of your class is:

class Node(object):
 pass

While the most Pythonic is:

class Node(object):
 def __init__(self, next, val):
 self.next = next
 self.val = val
lang-py

AltStyle によって変換されたページ (->オリジナル) /