Beginners question
Dave Angel
d at davea.name
Thu Aug 30 08:25:33 EDT 2012
On 08/30/2012 07:54 AM, boltar2003 at boltar.world wrote:
> Hello
>
> I'm slowly teaching myself python so apologies if this is a dumb question.
> but something has confused me with the os.stat() function:
>
>>>> s = os.stat(".")
>>>> print s
> posix.stat_result(st_mode=16877, st_ino=2278764L, st_dev=2053L, st_nlink=2, st_u
> id=1000, st_gid=100, st_size=4096L, st_atime=1346327745, st_mtime=1346327754, st
> _ctime=1346327754)
>
> What sort of object is posix.stat_result? Its not a dictionary or list or a
> class object as far as I can tell. Thanks for any help.
>
posix.stat_result is a class, and s is an instance of that class. You
can see that by typing type(s).
But you're wondering how print generated all that stuff about the s
instance. You can start to learn that with dir(s), which shows the
available attributes. All those attributes that have leading and
trailing double-underscores are called "special attributes," or "special
methods." In particular notice __str__(), which is a method provided
for your convenience. print will call that if it's available, when you
try to print an instance. It also masquerades as a tuple using
__getitem__() and other special methods.
Normal use of the instance is done by the attributes like s.st_atime
and s.st_size, or by using the object as a tuple. (using the square
brackets to fetch individual items or a range of items)
You can get more documentation directly from s by simply typing
help(s) and/or help(os.stat)
Or you can go to the web docs, http://docs.python.org/library/os.html
and search downward for os.stat (this link is currently for Python 2.7.3)
--
DaveA
More information about the Python-list
mailing list