Beginners question

B

boltar2003

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:
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
 
M

MRAB

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:

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:
<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.
 
R

Roy Smith

MRAB said:
What don't you ask Python? I'm sure you'' get something like this:

<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.
 
M

Marco Nawijn

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:




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.
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:
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:
.......
'__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:
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:

you could also check if it implements the dict interface:

Regards,

Marco
 
D

Dave Angel

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:

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)
 
C

Chris Angelico

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.)
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)
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:
16895

Hope that helps!

ChrisA
 
U

Ulrich Eckhardt

Am 30.08.2012 13:54, schrieb (e-mail address removed):
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
 
B

boltar2003

What don't you ask Python? I'm sure you'' get something like this:

<class 'posix.stat_result'>

Umm , no I don't.
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 '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?
... def __init__(self):
... pass
...<class '__main__.foo'>

B2003
 
C

Chris Angelico

Umm , no I don't.

<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?

.. def __init__(self):
.. pass
..
<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
 
B

boltar2003

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
 
D

Dave Angel

On Thu, 30 Aug 2012 13:14:57 +0100
<snip>
If its a class , why is it when I create my own class I get a completely
different output with print and type?

.. def __init__(self):
.. pass
..
<__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.
<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.
 
M

Marco Nawijn

Am 30.08.2012 13:54, schrieb (e-mail address removed):






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.
True
 
O

Oscar Benjamin

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
 
U

Ulrich Eckhardt

Am 30.08.2012 15:27, schrieb Marco Nawijn:
Am 30.08.2012 13:54, schrieb (e-mail address removed):
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.
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
 
H

Hans Mulder

Am 30.08.2012 13:54, schrieb (e-mail address removed):

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
 
T

Terry Reedy

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.
<class 'int'>
 
D

Dave Angel

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:

?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top