[Tutor] 'module' object has no attribute (setting a class attribute)
Steven D'Aprano
steve at pearwood.info
Sat Oct 16 08:33:34 CEST 2010
On Sat, 16 Oct 2010 08:20:37 am Tim Johnson wrote:
> I have two questions regarding this:
> 1)Am I using the correct method to set a class attribute?
Yes. The way to set a class attribute is the same way that you set an
attribute on *any* object:
some_object.attribute_name = something
In your case, some_object is the class object tmpl.tmpl
(Unlike some other languages, classes in Python are themselves objects.)
> 2)Is there a system configuration that would cause the
> AttributeError exception to print out the module name.
> (In this cause it was 'kbLib' not 'tmpl'.
No. The right way to fix this problem is to fix the error message, not
to create a system configuration option. What would you call it?
give_useless_error_messages = OFF
*wink*
The error messages in Python are not part of the language, but
implementation details. Unfortunately CPython (the version you are
almost certainly using) does have a few lousy error messages. However,
in this case it is fairly straightforward to guess what the problem
was. The error message you got was:
'module' object has no attribute 'templatepath'
On the left hand side, you had the module object tmpl, but you weren't
trying to set tmpl.templatepath (module object . attribute name), you
were trying tmpl.tmpl.templatepath (module object . class object .
attribute name). If the left hand side was the problem, you would
probably have got this error instead:
'module' object has no attribute 'tmpl'
(There are other errors you could have got, but they're fairly obscure
and/or unusual.)
--
Steven D'Aprano
More information about the Tutor
mailing list