Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Beginners question

Reply
Thread Tools

Beginners question

 
 
boltar2003@boltar.world
Guest
Posts: n/a
 
      08-30-2012
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

 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      08-30-2012
On 30/08/2012 12:54, http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
 
Roy Smith
Guest
Posts: n/a
 
      08-30-2012
In article <(E-Mail Removed)>,
MRAB <(E-Mail Removed)> 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.
 
Reply With Quote
 
Marco Nawijn
Guest
Posts: n/a
 
      08-30-2012
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
 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      08-30-2012
On 08/30/2012 07:54 AM, (E-Mail Removed) 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

 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      08-30-2012
On Thu, Aug 30, 2012 at 9:54 PM, <(E-Mail Removed)> 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
 
Reply With Quote
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      08-30-2012
Am 30.08.2012 13:54, schrieb (E-Mail Removed):
>>>> 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


 
Reply With Quote
 
boltar2003@boltar.world
Guest
Posts: n/a
 
      08-30-2012
On Thu, 30 Aug 2012 13:14:57 +0100
MRAB <(E-Mail Removed)> wrote:
>On 30/08/2012 12:54, (E-Mail Removed) 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

 
Reply With Quote
 
boltar2003@boltar.world
Guest
Posts: n/a
 
      08-30-2012
On Thu, 30 Aug 2012 08:25:33 -0400
Dave Angel <(E-Mail Removed)> 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


 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      08-30-2012
On Thu, Aug 30, 2012 at 10:50 PM, <(E-Mail Removed)> wrote:
> On Thu, 30 Aug 2012 13:14:57 +0100
> MRAB <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginners Question CHAZDG Wireless Networking 2 11-10-2005 12:29 AM
Beginners Question Regarding Static Web Pages Roger stevenson ASP .Net 8 07-01-2005 04:05 AM
Stupid beginners question: Windows Username Child ASP .Net 2 06-01-2005 08:03 PM
web service beginners question Mike P ASP .Net 3 09-21-2004 01:50 PM
Beginners question about DataGrid and page selection Alex ASP .Net 1 04-19-2004 08:36 PM



Advertisments