Beginners question

Discussion in 'Python' started by boltar2003@boltar.world, Aug 30, 2012.

  1. Guest

    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
    , Aug 30, 2012
    #1
    1. Advertising

  2. MRAB Guest

    On 30/08/2012 12:54, 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'>

    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.
    MRAB, Aug 30, 2012
    #2
    1. Advertising

  3. Roy Smith Guest

    In article <>,
    MRAB <> 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.
    Roy Smith, Aug 30, 2012
    #3
  4. Marco Nawijn Guest

    On Thursday, August 30, 2012 1:54:08 PM UTC+2, (unknown) 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 ora
    >
    > class object as far as I can tell. Thanks for any help.
    >
    >
    >
    > B2003


    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 thesource 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
    Marco Nawijn, Aug 30, 2012
    #4
  5. Dave Angel Guest

    On 08/30/2012 07:54 AM, 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
    Dave Angel, Aug 30, 2012
    #5
  6. On Thu, Aug 30, 2012 at 9:54 PM, <> 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
    Chris Angelico, Aug 30, 2012
    #6
  7. Am 30.08.2012 13:54, schrieb :
    >>>> 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
    Ulrich Eckhardt, Aug 30, 2012
    #7
  8. Guest

    On Thu, 30 Aug 2012 13:14:57 +0100
    MRAB <> wrote:
    >On 30/08/2012 12:54, 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
    , Aug 30, 2012
    #8
  9. Guest

    On Thu, 30 Aug 2012 08:25:33 -0400
    Dave Angel <> 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
    , Aug 30, 2012
    #9
  10. On Thu, Aug 30, 2012 at 10:50 PM, <> wrote:
    > On Thu, 30 Aug 2012 13:14:57 +0100
    > MRAB <> 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
    Chris Angelico, Aug 30, 2012
    #10
  11. Guest

    On Thu, 30 Aug 2012 23:06:34 +1000
    Chris Angelico <> 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
    , Aug 30, 2012
    #11
  12. Dave Angel Guest

    On 08/30/2012 08:50 AM, wrote:
    > On Thu, 30 Aug 2012 13:14:57 +0100
    > MRAB <> 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
    Dave Angel, Aug 30, 2012
    #12
  13. Marco Nawijn Guest

    On Thursday, August 30, 2012 3:15:03 PM UTC+2, Ulrich Eckhardt wrote:
    > Am 30.08.2012 13:54, schrieb :
    >
    > >>>> 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


    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
    Marco Nawijn, Aug 30, 2012
    #13
  14. On Thu, 30 Aug 2012 09:23:03 -0400, Dave Angel <> 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
    Oscar Benjamin, Aug 30, 2012
    #14
  15. 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 :
    >>> 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
    Ulrich Eckhardt, Aug 30, 2012
    #15
  16. Hans Mulder Guest

    On 30/08/12 14:49:54, Ulrich Eckhardt wrote:
    > Am 30.08.2012 13:54, schrieb :
    >>>>> 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.


    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
    Hans Mulder, Aug 30, 2012
    #16
  17. Terry Reedy Guest

    On 8/30/2012 9:30 AM, Oscar Benjamin wrote:
    > On Thu, 30 Aug 2012 09:23:03 -0400, Dave Angel <> 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).


    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
    Terry Reedy, Aug 30, 2012
    #17
  18. Guest

    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


    On Thursday, August 30, 2012 5:24:08 PM UTC+5:30, (unknown) 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.
    >
    >
    >
    > B2003
    , Sep 5, 2012
    #18
  19. Mark Lawrence, Sep 5, 2012
    #19
  20. Dave Angel Guest

    On 09/05/2012 04:03 AM, Mark Lawrence wrote:
    > On 05/09/2012 07:28, 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
    Dave Angel, Sep 5, 2012
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex
    Replies:
    1
    Views:
    275
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Apr 19, 2004
  2. Mike P

    web service beginners question

    Mike P, Aug 18, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    736
    S. Justin Gengo
    Sep 21, 2004
  3. Roger stevenson

    Beginners Question Regarding Static Web Pages

    Roger stevenson, Apr 30, 2005, in forum: ASP .Net
    Replies:
    8
    Views:
    396
    Sam Samnah
    Jul 1, 2005
  4. Child
    Replies:
    2
    Views:
    1,014
    souri challa
    Jun 1, 2005
  5. =?Utf-8?B?Qi4gQ2hlcm5pY2s=?=

    Late night beginners question

    =?Utf-8?B?Qi4gQ2hlcm5pY2s=?=, Jul 31, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    347
    =?Utf-8?B?Qi4gQ2hlcm5pY2s=?=
    Jul 31, 2005
Loading...

Share This Page