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
On Thu, 30 Aug 2012 23:06:34 +1000
Chris Angelico <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
Dave Angel
Guest
Posts: n/a
 
      08-30-2012
On 08/30/2012 08:50 AM, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> On Thu, 30 Aug 2012 13:14:57 +0100
> MRAB <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Marco Nawijn
Guest
Posts: n/a
 
      08-30-2012
On Thursday, August 30, 2012 3:15:03 PM UTC+2, Ulrich Eckhardt wrote:
> 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


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
 
Reply With Quote
 
Oscar Benjamin
Guest
Posts: n/a
 
      08-30-2012
On Thu, 30 Aug 2012 09:23:03 -0400, Dave Angel <(E-Mail Removed)> 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

 
Reply With Quote
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      08-30-2012
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 (E-Mail 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.
>
>>>> 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

 
Reply With Quote
 
Hans Mulder
Guest
Posts: n/a
 
      08-30-2012
On 30/08/12 14:49:54, Ulrich Eckhardt wrote:
> 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.


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


 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      08-30-2012
On 8/30/2012 9:30 AM, Oscar Benjamin wrote:
> On Thu, 30 Aug 2012 09:23:03 -0400, Dave Angel <(E-Mail Removed)> 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

 
Reply With Quote
 
charvigroups@gmail.com
Guest
Posts: n/a
 
      09-05-2012
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

 
Reply With Quote
 
Mark Lawrence
Guest
Posts: n/a
 
      09-05-2012
On 05/09/2012 07:28, (E-Mail Removed) 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.

 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      09-05-2012
On 09/05/2012 04:03 AM, Mark Lawrence wrote:
> On 05/09/2012 07:28, (E-Mail Removed) 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

 
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