Problem with script and typing

Wim Lavrijsen wlav at atlas03.cern.ch
Fri Jun 9 13:30:19 EDT 2000


olczyk at interaccess.com (Thaddeus L. Olczyk) writes:

I'm not quite sure what your code is supposed to do, so I'm just
guessing...

>project =[]

>class Project:
>   def IsNull(self):
>      return self.name==""
>      project.append(self)

I presume you meant 'global project.append( self )'?

>def FindProject(x):
>    for project in projects:
>        if project.name == x:
>            return x
>        null=Project("")
>        projects.remove(null)

I don't know what your trying to do here ... where does 'projects' come
from? It's never assigned an object.

>def is_project(s):
>    if s[len(s)-3:] == 'prj':
>        project=FindProject(s)
>## error here
>        if project.IsNull():
>            project=Project(s)
>        project.Scan()

Again ... 'global project'. Also, should this be a member of Project?

>----------
>error message AttributeError: 'None' object has no attribute 'IsNull'

And rightfully so! Sorry, but if you loop with project over projects
in FindProject to find a Project in project, someone is really going
to dislike you. :)

>2) Is there a better way of handling null objects? ( Most languages I 
>    know have objects which are explicitly null )

Yep, use None:

>>> project = None
>>> if project: print "there's a project!"
...
>>> if not project: print "no project!"
...
no project!

Best regards,
      Wim Lavrijsen

PS: you should use spaces instead of tabs.




More information about the Python-list mailing list