About getattr()

Samuel Karl Peterson skpeterson at nospam.please.ucdavis.edu
Mon Feb 12 00:05:49 EST 2007


"Jm lists" <practicalperl at gmail.com> on Mon, 12 Feb 2007 12:36:10
+0800 didst step forth and proclaim thus:

> Hello,
> 
> Since I can write the statement like:
> 
> >>> print os.path.isdir.__doc__
> Test whether a path is a directory
> 
> Why do I still need the getattr() func as below?
> 
> >>> print getattr(os.path,"isdir").__doc__
> Test whether a path is a directory

getattr lets you lookup an attribute given a string, so the attribute
wouldn't have to be hardcoded in your program, it could come from a
file, or from user input.

-- 
Sam Peterson
skpeterson At nospam ucdavis.edu
"if programmers were paid to remove code instead of adding it,
software would be much better" -- unknown



More information about the Python-list mailing list