A TYPICAL NEWBIE MISTAKE?

David Goodger dgoodger at bigfoot.com
Sun May 14 22:00:27 EDT 2000


on 2000-05-14 21:19, Courageous (jkraska1 at san.rr.com) wrote:
> To wit: append()ing to
> a list or assigning to a dictionary does not count
> as "assigned".
> 
> I spent quite a while this afternoon figuring this
> out. This must be a fairly common error. The solution
> is obvious. If you want to assign class variables as
> commentary the way I do, make sure you assign everyone
> in the __init__() method, ala:
> 
> class MyClass
> 
>  i=0
>  f=3.1
>  s="str"
>  ll=[]
> 
>  def __init__()
> 
>   i=0
>   f=3.1
>   s="str"
>   ll=[]

Actually, it should be:

  class MyClass:

    def __init__():
      self.i = 0
      self.f = 3.1
      self.s = "str"
      self.ll = []

Note that without the 'self.' in __init__() (or any other method), a
variable *local to that method* is created; the variables would disappear
upon the completion of the method.

> Albeit, now that I understand everything, this seems a
> bit reduntant. :)-

100% redundant, I think ;->

-- 
David Goodger    dgoodger at bigfoot.com    Open-source projects:
 - The Go Tools Project: http://gotools.sourceforge.net
 (more to come!)




More information about the Python-list mailing list