Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #28102 > unrolled thread
| Started by | boltar2003@boltar.world |
|---|---|
| First post | 2012-08-30 11:54 +0000 |
| Last post | 2012-09-05 09:21 -0400 |
| Articles | 18 — 11 participants |
Back to article view | Back to comp.lang.python
Beginners question boltar2003@boltar.world - 2012-08-30 11:54 +0000
Re: Beginners question MRAB <python@mrabarnett.plus.com> - 2012-08-30 13:14 +0100
Re: Beginners question Roy Smith <roy@panix.com> - 2012-08-30 08:23 -0400
Re: Beginners question boltar2003@boltar.world - 2012-08-30 12:50 +0000
Re: Beginners question Chris Angelico <rosuav@gmail.com> - 2012-08-30 23:06 +1000
Re: Beginners question boltar2003@boltar.world - 2012-08-30 13:16 +0000
Re: Beginners question Dave Angel <d@davea.name> - 2012-08-30 09:23 -0400
Re: Beginners question Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-08-30 14:30 +0100
Re: Beginners question Terry Reedy <tjreedy@udel.edu> - 2012-08-30 14:22 -0400
Re: Beginners question Dave Angel <d@davea.name> - 2012-08-30 08:25 -0400
Re: Beginners question boltar2003@boltar.world - 2012-08-30 12:53 +0000
Re: Beginners question Chris Angelico <rosuav@gmail.com> - 2012-08-30 22:32 +1000
Re: Beginners question Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-08-30 14:49 +0200
Re: Beginners question Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-08-30 16:41 +0200
Re: Beginners question Hans Mulder <hansmu@xs4all.nl> - 2012-08-30 17:38 +0200
Re: Beginners question charvigroups@gmail.com - 2012-09-04 23:28 -0700
Re: Beginners question Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-09-05 09:03 +0100
Re: Beginners question Dave Angel <d@davea.name> - 2012-09-05 09:21 -0400
| From | boltar2003@boltar.world |
|---|---|
| Date | 2012-08-30 11:54 +0000 |
| Subject | Beginners question |
| Message-ID | <k1nk8s$3l4$1@speranza.aioe.org> |
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
[toc] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2012-08-30 13:14 +0100 |
| Message-ID | <mailman.3964.1346328894.4697.python-list@python.org> |
| In reply to | #28102 |
On 30/08/2012 12:54, boltar2003@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'>
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.
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2012-08-30 08:23 -0400 |
| Message-ID | <roy-38D1EE.08232330082012@news.panix.com> |
| In reply to | #28105 |
In article <mailman.3964.1346328894.4697.python-list@python.org>, MRAB <python@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.
[toc] | [prev] | [next] | [standalone]
| From | boltar2003@boltar.world |
|---|---|
| Date | 2012-08-30 12:50 +0000 |
| Message-ID | <k1nnj1$cal$1@speranza.aioe.org> |
| In reply to | #28105 |
On Thu, 30 Aug 2012 13:14:57 +0100
MRAB <python@mrabarnett.plus.com> wrote:
>On 30/08/2012 12:54, boltar2003@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
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2012-08-30 23:06 +1000 |
| Message-ID | <mailman.3969.1346331996.4697.python-list@python.org> |
| In reply to | #28110 |
On Thu, Aug 30, 2012 at 10:50 PM, <boltar2003@boltar.world> wrote: > On Thu, 30 Aug 2012 13:14:57 +0100 > MRAB <python@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
[toc] | [prev] | [next] | [standalone]
| From | boltar2003@boltar.world |
|---|---|
| Date | 2012-08-30 13:16 +0000 |
| Message-ID | <k1np43$g8i$1@speranza.aioe.org> |
| In reply to | #28116 |
On Thu, 30 Aug 2012 23:06:34 +1000 Chris Angelico <rosuav@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
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <d@davea.name> |
|---|---|
| Date | 2012-08-30 09:23 -0400 |
| Message-ID | <mailman.3970.1346333002.4697.python-list@python.org> |
| In reply to | #28110 |
On 08/30/2012 08:50 AM, boltar2003@boltar.world wrote: > On Thu, 30 Aug 2012 13:14:57 +0100 > MRAB <python@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
[toc] | [prev] | [next] | [standalone]
| From | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
|---|---|
| Date | 2012-08-30 14:30 +0100 |
| Message-ID | <mailman.3971.1346333442.4697.python-list@python.org> |
| In reply to | #28110 |
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
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2012-08-30 14:22 -0400 |
| Message-ID | <mailman.3981.1346351013.4697.python-list@python.org> |
| In reply to | #28110 |
On 8/30/2012 9:30 AM, Oscar Benjamin wrote: > 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). 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
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <d@davea.name> |
|---|---|
| Date | 2012-08-30 08:25 -0400 |
| Message-ID | <mailman.3965.1346329560.4697.python-list@python.org> |
| In reply to | #28102 |
On 08/30/2012 07:54 AM, boltar2003@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
[toc] | [prev] | [next] | [standalone]
| From | boltar2003@boltar.world |
|---|---|
| Date | 2012-08-30 12:53 +0000 |
| Message-ID | <k1nno4$cj3$1@speranza.aioe.org> |
| In reply to | #28107 |
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
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2012-08-30 22:32 +1000 |
| Message-ID | <mailman.3966.1346329941.4697.python-list@python.org> |
| In reply to | #28102 |
On Thu, Aug 30, 2012 at 9:54 PM, <boltar2003@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
[toc] | [prev] | [next] | [standalone]
| From | Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> |
|---|---|
| Date | 2012-08-30 14:49 +0200 |
| Message-ID | <j444h9-tnp.ln1@satorlaser.homedns.org> |
| In reply to | #28102 |
Am 30.08.2012 13:54, schrieb boltar2003@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
[toc] | [prev] | [next] | [standalone]
| From | Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> |
|---|---|
| Date | 2012-08-30 16:41 +0200 |
| Message-ID | <fla4h9-caq.ln1@satorlaser.homedns.org> |
| In reply to | #28117 |
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 boltar2003@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
[toc] | [prev] | [next] | [standalone]
| From | Hans Mulder <hansmu@xs4all.nl> |
|---|---|
| Date | 2012-08-30 17:38 +0200 |
| Message-ID | <503f88fd$0$6989$e4fe514c@news2.news.xs4all.nl> |
| In reply to | #28117 |
On 30/08/12 14:49:54, Ulrich Eckhardt wrote:
> Am 30.08.2012 13:54, schrieb boltar2003@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.
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
[toc] | [prev] | [next] | [standalone]
| From | charvigroups@gmail.com |
|---|---|
| Date | 2012-09-04 23:28 -0700 |
| Message-ID | <782893e7-a2dc-4018-a5c6-2a9cd07ecf99@googlegroups.com> |
| In reply to | #28102 |
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
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2012-09-05 09:03 +0100 |
| Message-ID | <mailman.219.1346832107.27098.python-list@python.org> |
| In reply to | #28455 |
On 05/09/2012 07:28, charvigroups@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.
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <d@davea.name> |
|---|---|
| Date | 2012-09-05 09:21 -0400 |
| Message-ID | <mailman.236.1346851293.27098.python-list@python.org> |
| In reply to | #28455 |
On 09/05/2012 04:03 AM, Mark Lawrence wrote:
> On 05/09/2012 07:28, charvigroups@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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web