Help - Classes and attributes

Christopher Subich spam.csubich+block at block.subich.spam.com
Thu Jul 14 02:08:28 EDT 2005


rh0dium wrote:
> Hi all,
> 
> I believe I am having a fundamental problem with my class and I can't
> seem to figure out what I am doing wrong.  Basically I want a class
> which can do several specific ldap queries.  So in my code I would have
> multiple searches.  But I can't figure out how to do it without it
> barfing..
[snip]
>   File "./ldap-nsc.py", line 40, in search
>     ldap_result_id = l.search_s(baseDN, searchScope, searchAttrs,
> retrieveAttrs)
> AttributeError: NSCLdap instance has no attribute 'search_s'
> 
> 
> The code is also I believe straight forward..

You're going to kick yourself when you see the mistake.

> 
> import ldap
> 
> class NSCLdap:
> 
>     def __init__(self,server="sc-ldap.nsc.com"):
>         who=""; cred=""
>         self.server=server
>         try:
>             print "LDAP Version", ldap.__version__
>             l=ldap.open(server)
               ^^^^^^^^^^^^^^^^^^
[big snip]
> if __name__ == '__main__':
> 
>     l = NSCLdap()
>     l.search()

> I would love some pointers - clearly my code thinks that search_s is an
> attribute of my class but it's not..

Ah, but l -is- an instance of your class.  You want l to refer to the 
ldap connection, but you forgot do assign it to self.l -- in __init__, 
you assign l to simply a local variable, which goes poof as soon as 
__init__ returns.  You forgot the self.l throughout both __init__ and 
search.

You get the slighty misleading traceback because there is an "l" defined 
-- it just happens to be the one in globals(), the l = NSCLdap() that 
got assigned when you imported/ran the module.

Replace l = NSCLdap() with q = NSCLdap() (and l.search with q.search), 
and you'll get a NameError instead.



More information about the Python-list mailing list