Why self?

Louis M. Pecora pecora at anvil.nrl.navy.mil
Wed Jul 10 10:18:05 EDT 2002


In article <mailman.1026260895.5189.python-list at python.org>, Delaney,
Timothy <tdelaney at avaya.com> wrote:

> Why are you assigning None to the local variable "name", and then not using
> it?
> 
> That's the basic problem here - there is no way of knowing if "name" is
> supposed to be a local variable which will be used only in __init__, or
> whether you want to create an instance name.

Sigh.  Yes, it should be (in current Python)

    self.name=None

My point was then name can be used later in the object's methods
without the self.  

Sigh #2.  It was only a quick example.  I always initialize object
variables, usually with data that makes their use clear or their use
later not lead to problems.  Maybe something like,

    self.name="Default name"

I'm not advocating this as the end-all of "solutions", but just a quick
example that an alternative may be possible.

-- 
Lou Pecora
  - My views are my own.



More information about the Python-list mailing list