Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Beginners question

123 views
Skip to first unread message

bolta...@boltar.world

unread,
Aug 30, 2012, 7:54:05 AM8/30/12
to
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.

B2003

MRAB

unread,
Aug 30, 2012, 8:14:57 AM8/30/12
to pytho...@python.org
What don't you ask Python? I'm sure you'' get something like this:

>>> type(s)
<class 'posix.stat_result'>

In other words, it's an instance of the class "stat_result" as defined
in the file "posix.py".

On my system I get "<class 'nt.stat_result'>" because I'm using Windows.

Roy Smith

unread,
Aug 30, 2012, 8:23:23 AM8/30/12
to
In article <mailman.3964.1346328...@python.org>,
MRAB <pyt...@mrabarnett.plus.com> wrote:

> What don't you ask Python? I'm sure you'' get something like this:
>
> >>> type(s)
> <class 'posix.stat_result'>

BTW, this points out one of the really powerful aspects of Python. The
combination of introspection and a handy interactive interpreter makes
it easy to "just ask the computer".

It's often faster to play around with dir(), type(), and pprint() than
to find what you're looking for in the docs.

Marco Nawijn

unread,
Aug 30, 2012, 8:23:43 AM8/30/12
to
Hi,

So let's try to figure this out. First of all, we can ask Python what object it is.

>>> s = os.stat('.')
>>> type(s)
posix.stat_result

So it seems to be a custom type. However types can inherit from builtins like
list, tuple and dict, so maybe it still is a dict or a tuple. Let's ask Python again:

>>> isinstance(s, dict)
False
>>> isinstance(s, (tuple, list))
False

Ok. So it is neither a list (tuple) nor a dict. So without reverting to the source code, it is probably save to say that the result is a custom class where the attributes can be accessed by the dot '.' notation. This is confirmed when you do:

>>> dir(s)
......
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'n_fields',
'n_sequence_fields',
'n_unnamed_fields',
'st_atime',
'st_blksize',
'st_blocks',
'st_ctime',
'st_dev',
'st_gid',
'st_ino',
'st_mode',
'st_mtime',
'st_nlink',
'st_rdev',
'st_size',
'st_uid']

For example:

>>> print s.st_size
4096

In case of Linux I think that the result of os.stat(..) is a wrapping of a C struct (a class with only attributes and no methods).

A small additional remark. Besides being a real dict or list (by means of inheritance), custom class can also implement the interface (__getitem__ etc.). If you want to know if an object implements this interface you could use the types defined in the 'abc' and 'collections' standard modules. So instead of checking if a type is a dict like this:

>>> isinstance(s, dict)

you could also check if it implements the dict interface:

>>> isinstance(s, collections.MutableMapping) # or similar

Regards,

Marco

Dave Angel

unread,
Aug 30, 2012, 8:25:33 AM8/30/12
to bolta...@boltar.world, pytho...@python.org
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

Chris Angelico

unread,
Aug 30, 2012, 8:32:18 AM8/30/12
to pytho...@python.org
On Thu, Aug 30, 2012 at 9:54 PM, <bolta...@boltar.world> wrote:
> 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.

There's some cool things you can do here. (Note that I'm testing this
on a Windows box, so it's marginally different.)

>>> import os
>>> st=os.stat(".")
>>> st
nt.stat_result(st_mode=16895, st_ino=36873221949168842, st_dev=0,
st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1346329853,
st_mtime=1311543704, st_ctime=1306188101)
>>> help(st)

You'll get a couple of pages of help text about the object class that
the stat object is. You can do this with any object at all. Notably in
this case:

| This object may be accessed either as a tuple of
| (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)
| or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.

So, for instance:
>>> st[0]
16895
>>> st.st_mode
16895

Hope that helps!

ChrisA

bolta...@boltar.world

unread,
Aug 30, 2012, 8:50:41 AM8/30/12
to
On Thu, 30 Aug 2012 13:14:57 +0100
MRAB <pyt...@mrabarnett.plus.com> wrote:
>On 30/08/2012 12:54, bolta...@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.
>>
>What don't you ask Python? I'm sure you'' get something like this:
>
> >>> type(s)
><class 'posix.stat_result'>

Umm , no I don't.

>>> 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)
>>> type(s)
<type 'posix.stat_result'>

Which isn't terrible helpful.

>In other words, it's an instance of the class "stat_result" as defined
>in the file "posix.py".

If its a class , why is it when I create my own class I get a completely
different output with print and type?

>>>
>>> class foo(object):
.. def __init__(self):
.. pass
..
>>> f=foo()
>>> print f
<__main__.foo object at 0xb743956c>
>>> type(f)
<class '__main__.foo'>

B2003

bolta...@boltar.world

unread,
Aug 30, 2012, 8:53:24 AM8/30/12
to
On Thu, 30 Aug 2012 08:25:33 -0400
Dave Angel <d...@davea.name> wrote:
>You can get more documentation directly from s by simply typing
>help(s) and/or help(os.stat)

I didn't know about help(). Thanks!

B2003


Chris Angelico

unread,
Aug 30, 2012, 9:06:34 AM8/30/12
to pytho...@python.org
On Thu, Aug 30, 2012 at 10:50 PM, <bolta...@boltar.world> wrote:
> On Thu, 30 Aug 2012 13:14:57 +0100
> MRAB <pyt...@mrabarnett.plus.com> wrote:
>>What don't you ask Python? I'm sure you'' get something like this:
>>
>> >>> type(s)
>><class 'posix.stat_result'>
>
> Umm , no I don't.
>
>>>> type(s)
> <type 'posix.stat_result'>
>
> Which isn't terrible helpful.

That's actually the same thing, except for a slight difference between
Python 2 and Python 3.

> If its a class , why is it when I create my own class I get a completely
> different output with print and type?
>
>>>>
>>>> class foo(object):
> .. def __init__(self):
> .. pass
> ..
>>>> f=foo()
>>>> print f
> <__main__.foo object at 0xb743956c>
>>>> type(f)
> <class '__main__.foo'>

Yep, you're using Python 2. A few things are subtly different. Unless
you have good reason not to, do consider moving to Python 3; all sorts
of things are easier. Python 2 is basically not being developed any
more.

http://www.python.org/dev/peps/pep-0404/

Alternatively, accept that what people are going to quote to you here
may be slightly different from what you see.

In any case, Python's introspection facilities and help() features are
available on both branches, so most of what has been said in this
thread still applies.

ChrisA

Ulrich Eckhardt

unread,
Aug 30, 2012, 8:49:54 AM8/30/12
to
Am 30.08.2012 13:54, schrieb bolta...@boltar.world:
>>>> 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?

Use the type() function to find out. I guess that this is a named tuple,
which is a tuple where the attributes are not indexed but have a name,
see the documentation for the namedtuple() function from the collections
library.

Uli


bolta...@boltar.world

unread,
Aug 30, 2012, 9:16:51 AM8/30/12
to
On Thu, 30 Aug 2012 23:06:34 +1000
Chris Angelico <ros...@gmail.com> wrote:
>Yep, you're using Python 2. A few things are subtly different. Unless
>you have good reason not to, do consider moving to Python 3; all sorts

Noted. Thanks.

B2003


Dave Angel

unread,
Aug 30, 2012, 9:23:03 AM8/30/12
to pytho...@python.org
On 08/30/2012 08:50 AM, bolta...@boltar.world wrote:
> On Thu, 30 Aug 2012 13:14:57 +0100
> MRAB <pyt...@mrabarnett.plus.com> wrote:
> <snip>
> If its a class , why is it when I create my own class I get a completely
> different output with print and type?
>
>>>> class foo(object):
> .. def __init__(self):
> .. pass
> ..
>>>> f=foo()
>>>> print f
> <__main__.foo object at 0xb743956c>

You get that because you didn't provide a __str__() method in your
class. As i said in my other message, posix.stat_result is providing
that capability for your debugging convenience. There's no requirement
to provide it, but that's why the difference.

>>>> type(f)
> <class '__main__.foo'>
>
>
>

I haven't discovered why sometimes the type output shows type instead of
class. There are other ways of defining classes, however, and perhaps
this is using one of them. Still, it is a class, and stat() is
returning an instance of that class.

--

DaveA

Marco Nawijn

unread,
Aug 30, 2012, 9:27:44 AM8/30/12
to
It is not a namedtuple. Because a namedtuple "is" a tuple and therefore isinstance(s, tuple) would have returned True.

>>> from collections import namedtuple
>>> Point = namedtuple('Point', 'x y')
>>> p = Point(10,2)
>>> isinstance(p, tuple)
True

Oscar Benjamin

unread,
Aug 30, 2012, 9:30:29 AM8/30/12
to pytho...@python.org
On Thu, 30 Aug 2012 09:23:03 -0400, Dave Angel <d...@davea.name> wrote:
> I haven't discovered why sometimes the type output shows type
instead of
> class. There are other ways of defining classes, however, and
perhaps
> this is using one of them. Still, it is a class, and stat() is
> returning an instance of that class.

Builtin types show as type and classes defined in python show as
class (even if they inherit from builtin types).

Oscar

Ulrich Eckhardt

unread,
Aug 30, 2012, 10:41:19 AM8/30/12
to
Am 30.08.2012 15:27, schrieb Marco Nawijn:
> On Thursday, August 30, 2012 3:15:03 PM UTC+2, Ulrich Eckhardt wrote:
>> Am 30.08.2012 13:54, schrieb bolta...@boltar.world:
>>> What sort of object is posix.stat_result?
[...]
>> I guess that this is a named tuple, which is a tuple where the
>> attributes are not indexed but have a name, see the
>> documentation for the namedtuple() function from the collections
>> library.
>>
>
> It is not a namedtuple. Because a namedtuple "is" a tuple and therefore isinstance(s, tuple) would have returned True.
>
>>>> from collections import namedtuple
>>>> Point = namedtuple('Point', 'x y')
>>>> p = Point(10,2)
>>>> isinstance(p, tuple)
> True

Hi Marco,

I don't find anything wrong with what you say, the output formatting
from using a type created by namedtuple would have been slightly
different indeed. However, I also don't understand the point you're
trying to make, in particular why it matters that a namedtuple type is
derived from tuple, other than perhaps that access by name is available
in addition to access by index.

Greetings!

Uli

Hans Mulder

unread,
Aug 30, 2012, 11:38:37 AM8/30/12
to
Named tuples were invented to do this kind of thing.

However, stat_result is fairly old, and named tuples
had not been invented back then.

If named tuples had been invented first, then os.stat
would probably have used them.

Hope this helps,

-- HansM


Terry Reedy

unread,
Aug 30, 2012, 2:22:45 PM8/30/12
to pytho...@python.org
Only in 2.x, and this goes back to the old user class system, which the
OP should not have to learn about.

>>> type(1)
<class 'int'>


--
Terry Jan Reedy

charvi...@gmail.com

unread,
Sep 5, 2012, 2:28:25 AM9/5/12
to
Hi,

I have attached python interview questions and answers for beginners.

Please visit http://www.f2finterview.com/web/CorePython/ for core python and

http://www.f2finterview.com/web/PythonAdvanced/ for advanced python

Mark Lawrence

unread,
Sep 5, 2012, 4:03:00 AM9/5/12
to pytho...@python.org
On 05/09/2012 07:28, charvi...@gmail.com wrote:
> Hi,
>
> I have attached python interview questions and answers for beginners.
>
> Please visit http://www.f2finterview.com/web/CorePython/ for core python and
>
> http://www.f2finterview.com/web/PythonAdvanced/ for advanced python
>
>

The first question from the advanced list is really going to stretch an
advanced Python developer, so only gurus need bother as it's so
difficult. Not.


--
Cheers.

Mark Lawrence.

Dave Angel

unread,
Sep 5, 2012, 9:21:11 AM9/5/12
to Mark Lawrence, pytho...@python.org
On 09/05/2012 04:03 AM, Mark Lawrence wrote:
> On 05/09/2012 07:28, charvi...@gmail.com wrote:
>> Hi,
>>
>> I have attached python interview questions and answers for beginners.
>>
>> Please visit http://www.f2finterview.com/web/CorePython/ for core
>> python and
>>
>> http://www.f2finterview.com/web/PythonAdvanced/ for advanced python
>>
>>
>
> The first question from the advanced list is really going to stretch
> an advanced Python developer, so only gurus need bother as it's so
> difficult. Not.
>
>

If the interviewer wants the whole page, and not just the first line,
then there's some understanding needed there. What bothers me more is
the provided code and description:

for c in xrange(len(records)):
fvalues = records[c]
...

and

"Here we start a loop which starts from 1 (understood) to whatever the ..."

Isn't an "advanced" Python user going to be expected to replace those
two with

for fvalues in records:

?


--

DaveA

0 new messages