By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,525 Members | 1,634 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,525 IT Pros & Developers. It's quick & easy.

Structures

P: n/a
Hi all,

I am a Python beginner, reading through 2.6 tutorial. I am wondering
where are structures?

On the other hand, I think I might have the answer. Since Python focus
on having one way to do it and structures are something like classes
with only public methods, if I want structures that's what I should use.
Is that right?

Cheers,
--
Paulo Jorge Matos - pocmatos at gmail.com
Webpage: http://www.personal.soton.ac.uk/pocm
Nov 3 '08 #1
Share this Question
Share on Google+
32 Replies


P: n/a
Paulo J. Matos:
Since Python focus
on having one way to do it and structures are something like classes
with only public methods, if I want structures that's what I should use.
Is that right?
Yes, it is. On the other hand in Python 2.6 there's something that
helps you build one of such classes in a standard and handy way: see
"namedtuple" in the standard "collections" module.

Bye,
bearophile
Nov 3 '08 #2

P: n/a
"Paulo J. Matos" <po******@gmail.comwrites:
I am a Python beginner, reading through 2.6 tutorial. I am wondering
where are structures?
I'm wondering a more fundamental question: What are structures? That
is, what do *you* mean by that term; without knowing that, an answer
isn't likely to be meaningful.
[…] structures are something like classes with only public methods
Care to say more about what they are, not what they're like?

--
\ “Pinky, are you pondering what I'm pondering?” “Yes Brain, but |
`\ if our knees bent the other way, how would we ride a bicycle?” |
_o__) —_Pinky and The Brain_ |
Ben Finney
Nov 3 '08 #3

P: n/a
On Mon, Nov 3, 2008 at 12:32 PM, Ben Finney
<bi****************@benfinney.id.auwrote:
"Paulo J. Matos" <po******@gmail.comwrites:
>I am a Python beginner, reading through 2.6 tutorial. I am wondering
where are structures?

I'm wondering a more fundamental question: What are structures? That
is, what do *you* mean by that term; without knowing that, an answer
isn't likely to be meaningful.
>[] structures are something like classes with only public methods

Care to say more about what they are, not what they're like?
Well, I guess that everyone pretty much gets since it exists in every
other language as struct, or define-structure, or whatever is the
syntax. Still, answering your rhetoric question, a structure is way to
gather information by fields and those fields are referenced by name.
The fact that python 2.6 has now named tuples is a breath of fresh
air!
--
\ "Pinky, are you pondering what I'm pondering?" "Yes Brain, but |
`\ if our knees bent the other way, how would we ride a bicycle?" |
_o__) _Pinky and The Brain_ |
Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list


--
Paulo Jorge Matos - pocmatos at gmail.com
Webpage: http://www.personal.soton.ac.uk/pocm
Nov 3 '08 #4

P: n/a
"Paulo J. Matos" <po******@gmail.comwrites:
On Mon, Nov 3, 2008 at 12:32 PM, Ben Finney
<bi****************@benfinney.id.auwrote:
I'm wondering a more fundamental question: What are structures?
That is, what do *you* mean by that term; without knowing that, an
answer isn't likely to be meaningful.

Well, I guess that everyone pretty much gets since it exists in
every other language as struct, or define-structure, or whatever is
the syntax.
Take care with broad sweeping statements about “every other language”,
or even “most other languages”. They are usually flat-out wrong:
there is a stunning variety of different approaches and concepts in
programming languages, with very little common to even a majority of
them.

So no, the question was entirely honest and not a rhetorical device.
Still, answering your rhetoric question, a structure is way to
gather information by fields and those fields are referenced by
name.
Okay, you're talking about ‘struct’ from the C language. That helps
answer the question.

In Python, the way to do that is with a dict. A class can be used, but
is often overkill if one doesn't need customised behaviour.
The fact that python 2.6 has now named tuples is a breath of fresh
air!
That works also, but a dict will be more broadly useful; and
compatible with any Python version.

--
\ “Creativity can be a social contribution, but only in so far as |
`\ society is free to use the results.” —Richard Stallman |
_o__) |
Ben Finney
Nov 3 '08 #5

P: n/a
On Mon, 3 Nov 2008 20:33:45 +0000, Paulo J. Matos <po******@gmail.comwrote:
On Mon, Nov 3, 2008 at 12:32 PM, Ben Finney
<bi****************@benfinney.id.auwrote:
>"Paulo J. Matos" <po******@gmail.comwrites:
....
>I'm wondering a more fundamental question: What are structures? That
is, what do *you* mean by that term; without knowing that, an answer
isn't likely to be meaningful.
>>[?] structures are something like classes with only public methods

Care to say more about what they are, not what they're like?

Well, I guess that everyone pretty much gets since it exists in every
other language as struct, or define-structure, or whatever is the
syntax.
You're right. But the explanation you gave above, "classes with only
public methods", is C++-specific, and also fairly misleading ... there
is no real difference in C++ between struct Foo {}; and a class Foo
{}; it's just shorthand for class Foo { public: }; and struct Foo {
private: }; respectively.

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.se R'lyeh wgah'nagl fhtagn!
Nov 3 '08 #6

P: n/a
Ben Finney <bi****************@benfinney.id.auwrites:
"Paulo J. Matos" <po******@gmail.comwrites:
[...]
Okay, you're talking about ‘struct’ from the C language. That helps
answer the question.
Note that structs are mutable.
In Python, the way to do that is with a dict. A class can be used, but
is often overkill if one doesn't need customised behaviour.
>The fact that python 2.6 has now named tuples is a breath of fresh
air!
But isn't mutable, so it doesn't seem to be what you (Paulo) need.
That works also, but a dict will be more broadly useful; and
compatible with any Python version.
And is mutable.

--
Arnaud
Nov 3 '08 #7

P: n/a
On Nov 3, 3:45*pm, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:
"Paulo J. Matos" <pocma...@gmail.comwrites:
On Mon, Nov 3, 2008 at 12:32 PM, Ben Finney
<bignose+hates-s...@benfinney.id.auwrote:
I'm wondering a more fundamental question: What are structures?
That is, what do *you* mean by that term; without knowing that, an
answer isn't likely to be meaningful.
Well, I guess that everyone pretty much gets since it exists in
every other language as struct, or define-structure, or whatever is
the syntax.

Take care with broad sweeping statements about every other language,
or even most other languages. They are usually flat-out wrong:
there is a stunning variety of different approaches and concepts in
programming languages, with very little common to even a majority of
them.
Yea, verily. How many languages do you think that is? Feel free to
count C and C++ as different ones.

"Four shalt thou not count, neither count thou two...."
http://en.wikipedia.org/wiki/Holy_Ha...e_instructions

Nov 3 '08 #8

P: n/a
>
Care to say more about what they are, not what they're like?
I'm not the OP and I may be biased by C++, I can imagine the
complaints when I say, classes are just structures with function
members for working on the structure.
Nov 3 '08 #9

P: n/a
Craig Allen <ca*******@gmail.comwrites:
>>
Care to say more about what they are, not what they're like?

I'm not the OP and I may be biased by C++, I can imagine the
complaints when I say, classes are just structures with function
members for working on the structure.
In C++ classes and structures are the same, except that class members
are private by default and structure members are public by default

--
Arnaud
Nov 3 '08 #10

P: n/a
On Mon, Nov 3, 2008 at 10:10 PM, Arnaud Delobelle
<ar*****@googlemail.comwrote:
Ben Finney <bi****************@benfinney.id.auwrites:
>"Paulo J. Matos" <po******@gmail.comwrites:
[...]
>Okay, you're talking about 'struct' from the C language. That helps
answer the question.

Note that structs are mutable.
Ah... :( That's a no go!
>In Python, the way to do that is with a dict. A class can be used, but
is often overkill if one doesn't need customised behaviour.
>>The fact that python 2.6 has now named tuples is a breath of fresh
air!

But isn't mutable, so it doesn't seem to be what you (Paulo) need.
What's then the reason for adding named tuples if they are not mutable...???
>That works also, but a dict will be more broadly useful; and
compatible with any Python version.

And is mutable.
Even though I can use dicts where the keys are strings (as if it were
the name of the field), it seems to heavy, since a structure doesn't
need to be resizable (and dicts are) and it has constant time access
(which depending on the implementation I would guess dicts don't
have).

Can someone please clarify?

Cheers,

Paulo
--
Arnaud
--
http://mail.python.org/mailman/listinfo/python-list


--
Paulo Jorge Matos - pocmatos at gmail.com
Webpage: http://www.personal.soton.ac.uk/pocm
Nov 3 '08 #11

P: n/a
On Mon, Nov 3, 2008 at 10:19 PM, Aaron Brady <ca********@gmail.comwrote:
On Nov 3, 3:45 pm, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:
>"Paulo J. Matos" <pocma...@gmail.comwrites:
On Mon, Nov 3, 2008 at 12:32 PM, Ben Finney
<bignose+hates-s...@benfinney.id.auwrote:
I'm wondering a more fundamental question: What are structures?
That is, what do *you* mean by that term; without knowing that, an
answer isn't likely to be meaningful.
Well, I guess that everyone pretty much gets since it exists in
every other language as struct, or define-structure, or whatever is
the syntax.

Take care with broad sweeping statements about "every other language",
or even "most other languages". They are usually flat-out wrong:
there is a stunning variety of different approaches and concepts in
programming languages, with very little common to even a majority of
them.

Yea, verily. How many languages do you think that is? Feel free to
count C and C++ as different ones.

"Four shalt thou not count, neither count thou two...."
http://en.wikipedia.org/wiki/Holy_Ha...e_instructions
Well, I wouldn't dare to say I know a lot of languages but the ones I
do provide mechanisms to define structures / records: C, C++, Scheme,
Common Lisp, Haskell, SML, Ocaml.
This is obviously a minority if you count all available programming
languages in the world, but I would dare to say these cover a lot of
ground.

However, I wouldn't dare to say Python needs structures to be a good
language, or anything similar. My question was more directed to : if
there aren't structures in Python, what do Pythonists use instead?
(I have seen dicts might be an alternative, but as I said in previous
post, they seem to flexible [making them a canon to shoot a fly, and
they probably lack constant-time access, right?]
--
Paulo Jorge Matos - pocmatos at gmail.com
Webpage: http://www.personal.soton.ac.uk/pocm
Nov 3 '08 #12

P: n/a
On Nov 3, 2008, at 4:38 PM, Paulo J. Matos wrote:
However, I wouldn't dare to say Python needs structures to be a good
language, or anything similar. My question was more directed to : if
there aren't structures in Python, what do Pythonists use instead?
Classes.

Best,
- Joe
Nov 3 '08 #13

P: n/a
On Mon, Nov 3, 2008 at 11:47 PM, Joe Strout <jo*@strout.netwrote:
On Nov 3, 2008, at 4:38 PM, Paulo J. Matos wrote:
>However, I wouldn't dare to say Python needs structures to be a good
language, or anything similar. My question was more directed to : if
there aren't structures in Python, what do Pythonists use instead?

Classes.
ok, so that goes back to what I said on my first post. Classes with no
private fields/methods.

Thanks,

Paulo
Best,
- Joe
--
http://mail.python.org/mailman/listinfo/python-list


--
Paulo Jorge Matos - pocmatos at gmail.com
Webpage: http://www.personal.soton.ac.uk/pocm
Nov 3 '08 #14

P: n/a
On Nov 3, 6:51*pm, "Paulo J. Matos" <pocma...@gmail.comwrote:
On Mon, Nov 3, 2008 at 11:47 PM, Joe Strout <j...@strout.netwrote:
On Nov 3, 2008, at 4:38 PM, Paulo J. Matos wrote:
However, I wouldn't dare to say Python needs structures to be a good
language, or anything similar. My question was more directed to : if
there aren't structures in Python, what do Pythonists use instead?
Classes.

ok, so that goes back to what I said on my first post. Classes with no
private fields/methods.
Technically there are no private attributes in (pure) Python so the
answer is still classes.

George
Nov 4 '08 #15

P: n/a
On Mon, 03 Nov 2008 23:32:25 +0000, Paulo J. Matos wrote:
What's then the reason for adding named tuples if they are not
mutable...???
Names are more descriptive than "magic numbers" as indices. See for
example the "named tuple" returned by `os.stat()`.

Ciao,
Marc 'BlackJack' Rintsch
Nov 4 '08 #16

P: n/a
On Tue, Nov 4, 2008 at 12:17 AM, George Sakkis <ge***********@gmail.comwrote:
>
Technically there are no private attributes in (pure) Python so the
answer is still classes.
OK, so this is now messing with my lack of knowledge regarding Python.
What's (pure) Python? Is there any impure Python?
George
--
http://mail.python.org/mailman/listinfo/python-list


--
Paulo Jorge Matos - pocmatos at gmail.com
Webpage: http://www.personal.soton.ac.uk/pocm
Nov 4 '08 #17

P: n/a
"Paulo J. Matos" <po******@gmail.comwrites:
OK, so this is now messing with my lack of knowledge regarding Python.
What's (pure) Python? Is there any impure Python?
impure Python = Python with extensions written in C.
Nov 4 '08 #18

P: n/a
On Nov 3, 6:32*pm, "Paulo J. Matos" <pocma...@gmail.comwrote:
Even though I can use dicts where the keys are strings (as if it were
the name of the field), it seems to heavy, since a structure doesn't
need to be resizable (and dicts are) and it has constant time access
(which depending on the implementation I would guess dicts don't
have).

Can someone please clarify?
For all practical purposes, dicts have almost constant access time (at
least with any half-decent __hash__ method). If you're paranoid about
micro-optimizations, you can define a class with __slots__:
>>class MyStruct(object):
.... __slots__ = ('x', 'y')
....
>>s = MyStruct()
s.x = 3
s.y = 4
s.z= 5
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'MyStruct' object has no attribute 'z'

More often than not, that's premature optimization.

George
Nov 4 '08 #19

P: n/a
On Nov 3, 6:33*pm, George Sakkis <george.sak...@gmail.comwrote:
On Nov 3, 6:32*pm, "Paulo J. Matos" <pocma...@gmail.comwrote:
Even though I can use dicts where the keys are strings (as if it were
the name of the field), it seems to heavy, since a structure doesn't
need to be resizable (and dicts are) and it has constant time access
(which depending on the implementation I would guess dicts don't
have).
Can someone please clarify?

For all practical purposes, dicts have almost constant access time (at
least with any half-decent __hash__ *method).
Hash tables are slick, but their hash function is their weakest link.
>>[ hash( 2**x ) for x in range( 0, 256, 32 ) ]
[1, 1, 1, 1, 1, 1, 1, 1]

Such an index gives you linear-time performance in finding elements.
Ha! Plus, your worst-case insertion will cause a copy of the entire
table. There aren't any mappings based on balanced trees,
unfortunately.
Nov 4 '08 #20

P: n/a
On Nov 3, 5:38*pm, "Paulo J. Matos" <pocma...@gmail.comwrote:
On Mon, Nov 3, 2008 at 10:19 PM, Aaron Brady <castiro...@gmail.comwrote:
On Nov 3, 3:45 pm, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:
"Paulo J. Matos" <pocma...@gmail.comwrites:
Take care with broad sweeping statements about "every other language",
or even "most other languages". They are usually flat-out wrong:
there is a stunning variety of different approaches and concepts in
programming languages, with very little common to even a majority of
them.
Yea, verily. *How many languages do you think that is? *Feel free to
count C and C++ as different ones.

Well, I wouldn't dare to say I know a lot of languages but the ones I
do provide mechanisms to define structures / records: C, C++, Scheme,
Common Lisp, Haskell, SML, Ocaml.
I don't know even half of those. What about Perl? Does anyone know
that?
My question was more directed to : if
there aren't structures in Python, what do Pythonists use instead?
Just a blank object, I imagine.
Nov 4 '08 #21

P: n/a
On approximately 11/3/2008 3:38 PM, came the following characters from
the keyboard of Paulo J. Matos:
On Mon, Nov 3, 2008 at 10:19 PM, Aaron Brady <ca********@gmail.comwrote:
>On Nov 3, 3:45 pm, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:
>>"Paulo J. Matos" <pocma...@gmail.comwrites:
On Mon, Nov 3, 2008 at 12:32 PM, Ben Finney
<bignose+hates-s...@benfinney.id.auwrote:

I'm wondering a more fundamental question: What are structures?
That is, what do *you* mean by that term; without knowing that, an
answer isn't likely to be meaningful.
>
Well, I guess that everyone pretty much gets since it exists in
every other language as struct, or define-structure, or whatever is
the syntax.

Take care with broad sweeping statements about "every other language",
or even "most other languages". They are usually flat-out wrong:
there is a stunning variety of different approaches and concepts in
programming languages, with very little common to even a majority of
them.
Yea, verily. How many languages do you think that is? Feel free to
count C and C++ as different ones.

"Four shalt thou not count, neither count thou two...."
http://en.wikipedia.org/wiki/Holy_Ha...e_instructions

Well, I wouldn't dare to say I know a lot of languages but the ones I
do provide mechanisms to define structures / records: C, C++, Scheme,
Common Lisp, Haskell, SML, Ocaml.
This is obviously a minority if you count all available programming
languages in the world, but I would dare to say these cover a lot of
ground.
There are languages that do not have structs; but usually there is some
way to obtain something similar.
However, I wouldn't dare to say Python needs structures to be a good
language, or anything similar. My question was more directed to : if
there aren't structures in Python, what do Pythonists use instead?
(I have seen dicts might be an alternative, but as I said in previous
post, they seem to flexible [making them a canon to shoot a fly, and
they probably lack constant-time access, right?]
Dicts have constant time access. On the other hand, the constant is
significantly larger than the constant for accessing a C struct.

Note that classes, by default, are based on a contained dict! There are
games to be played with slots that can apparently improve that. I am
not yet experienced enough with Python to know if a slot is as fast as a
C struct, but perhaps it is. You can have both slots and a dict, to get
both speed and flexibility, or you can eliminate the dict and use slots
only, but that limits your flexibility. But structs aren't flexible,
except at compile time, so that might not be a problem for you.

Another thing I don't know is if slots are as fast as tuples. Perhaps a
slots-only class and a tuple might be speed rivals? But the former is
mutable, and the latter not.

Perhaps a more experience Python user can answer that question, at least
for some particular implementation.

--
Glenn -- http://nevcal.com/
===========================
A protocol is complete when there is nothing left to remove.
-- Stuart Cheshire, Apple Computer, regarding Zero Configuration Networking

Nov 4 '08 #22

P: n/a
On Mon, 03 Nov 2008 17:06:07 -0800, Aaron Brady wrote:
>For all practical purposes, dicts have almost constant access time (at
least with any half-decent __hash__ *method).

Hash tables are slick, but their hash function is their weakest link.
>>>[ hash( 2**x ) for x in range( 0, 256, 32 ) ]
[1, 1, 1, 1, 1, 1, 1, 1]
That's not the hash function of a dict. Dicts don't have a hash function:
>>hash({})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: dict objects are unhashable
What you're showing is the hash function of ints, and you've discovered
that there are some collisions. This is hardly surprising. There are an
infinite number of ints, and only a finite number of values that they can
hash to, so there have to be some collisions. It's worth looking at the
values that collide:
>>[ 2**x for x in range( 0, 256, 32 ) ]
[1, 4294967296L, 18446744073709551616L, 79228162514264337593543950336L,
340282366920938463463374607431768211456L,
1461501637330902918203684832716283019655932542976L ,
62771017353866807638357894232076664161023554444640 34512896L,
26959946667150639794667015087019630673637144422540 572481103610249216L]

So if you have a dict with keys equal to those numbers, and do a lookup
on one of them, you'll get O(N) performance instead of O(1) *for those
keys that collide*. I think I can live with that.

If you think you can come up with a better hash function, feel free to
subclass int.

Such an index gives you linear-time performance in finding elements. Ha!
In any real application, all your keys are highly unlikely to collide in
that fashion.
Plus, your worst-case insertion will cause a copy of the entire table.
Explain please.

There aren't any mappings based on balanced trees, unfortunately.
I'm not sure why you think O(log N) is better than O(1). The major
advantage of tree structures is that, unlike hash tables, they are
ordered, not that they don't have collisions.

--
Steven
Nov 4 '08 #23

P: n/a
On Tue, 04 Nov 2008 00:19:16 +0000, Marc 'BlackJack' Rintsch wrote:
On Mon, 03 Nov 2008 23:32:25 +0000, Paulo J. Matos wrote:
>What's then the reason for adding named tuples if they are not
mutable...???

Names are more descriptive than "magic numbers" as indices. See for
example the "named tuple" returned by `os.stat()`.

I have no objection to named tuples, but I've sometimes missed having an
equivalent to the Pascal record or C struct: essentially a named mutable
tuple. Here's a quick way to get one:

def record(**kwargs):
"""Lightweight named mutable tuple equivalent."""
class Record(object):
__slots__ = kwargs.keys()
x = Record()
for key, value in kwargs.items():
setattr(x, key, value)
return x
It needs more work to be a full-fledged record, e.g. __eq__ and __str__,
but it's a start.
--
Steven
Nov 4 '08 #24

P: n/a
On approximately 11/3/2008 5:28 PM, came the following characters from
the keyboard of Aaron Brady:
On Nov 3, 5:38 pm, "Paulo J. Matos" <pocma...@gmail.comwrote:
>On Mon, Nov 3, 2008 at 10:19 PM, Aaron Brady <castiro...@gmail.comwrote:
>>On Nov 3, 3:45 pm, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:

"Paulo J. Matos" <pocma...@gmail.comwrites:
Take care with broad sweeping statements about "every other language",
or even "most other languages". They are usually flat-out wrong:
there is a stunning variety of different approaches and concepts in
programming languages, with very little common to even a majority of
them.

Yea, verily. How many languages do you think that is? Feel free to
count C and C++ as different ones.
Well, I wouldn't dare to say I know a lot of languages but the ones I
do provide mechanisms to define structures / records: C, C++, Scheme,
Common Lisp, Haskell, SML, Ocaml.

I don't know even half of those. What about Perl? Does anyone know
that?
structs in Perl are generally implemented as hashes, which is similar to
a Python dict.

--
Glenn -- http://nevcal.com/
===========================
A protocol is complete when there is nothing left to remove.
-- Stuart Cheshire, Apple Computer, regarding Zero Configuration Networking

Nov 4 '08 #25

P: n/a
On Nov 4, 2:57*am, Glenn Linderman <v+pyt...@g.nevcal.comwrote:
Note that classes, by default, are based on a contained dict! *There are
games to be played with slots that can apparently improve that. *I am
not yet experienced enough with Python to know if a slot is as fast as a
C struct, but perhaps it is. *
No, slots have nothing to do with speed, they are a memory
optimization. IMO slots fall in the category of premature optimization
and it was a mistake to include them in the language
(the functionality should have been made available only at the C-
level). As of now, lots of people abuse slots without realizing their
disadvantages (for instance, they break multiple inheritance).
Nov 4 '08 #26

P: n/a
Joe Strout a crit :
On Nov 3, 2008, at 4:38 PM, Paulo J. Matos wrote:
>However, I wouldn't dare to say Python needs structures to be a good
language, or anything similar. My question was more directed to : if
there aren't structures in Python, what do Pythonists use instead?

Classes.
or objects, or dicts, depending on concrete use case, personnal
preferences and current phase of the moon.

Nov 4 '08 #27

P: n/a
Paulo J. Matos a crit :
(snip)
However, I wouldn't dare to say Python needs structures to be a good
language, or anything similar. My question was more directed to : if
there aren't structures in Python, what do Pythonists use instead?
(I have seen dicts might be an alternative,
Yes, and the most obvious one - at least when all you need is a kind of
data transfert object. Else, well, C++ structs are just a survival of a
C feature kept for compatibility reasons - technically speaking, you
just don't need structs when you have classes.
but as I said in previous
post, they seem to flexible [making them a canon to shoot a fly,
Err... I'm afraid you'll find Python way to flexible and dangerous,
then. You may not be aware of the fact that you can add / remove /
replace objects attributes (and methods) at runtime ? FWIW, and a couple
corner cases set asides, Python objects are mostly glorified dicts.
and
they probably lack constant-time access, right?]
AFAIK, it's almost constant-time access. But you won't get anything
better using classes, since attributes are stored in a dict anyway.

Nov 4 '08 #28

P: n/a
Michele Simionato:
No, slots have nothing to do with speed, they are a memory optimization.
In many languages, often in Python too, the less memory you use/
allocate the faster you go.

In fact slots allow a speed increase too (in new style classes):

from timeit import default_timer as clock

class C1(object):
__slots__ = ["a", "b"]
def __init__(self, a, b):
self.a = a
self.a = b

class C2(object):
def __init__(self, a, b):
self.a = a
self.a = b

def main(N, test):
t0 = clock()

if test == 1:
[C1(ab, ab) for ab in xrange(N)]
elif test == 2:
[C2(ab, ab) for ab in xrange(N)]

t1 = clock()
print round(t1 - t0, 2)

main(N=700*1000, test=1)

Core duo 2 GHz:
test=1 ==1.06 s
test=2 ==3.0 s

(700*1000 is the way I have found to write the 700_000 I was talking
about, until we'll have a syntax for it.)

Bye,
bearophile
Nov 4 '08 #29

P: n/a
On Nov 4, 11:20*am, bearophileH...@lycos.com wrote:
Michele Simionato:
No, slots have nothing to do with speed, they are a memory optimization..

In many languages, often in Python too, the less memory you use/
allocate the faster you go.

In fact slots allow a speed increase too (in new style classes):

from timeit import default_timer as clock

class C1(object):
* * __slots__ = ["a", "b"]
* * def __init__(self, a, b):
* * * * self.a = a
* * * * self.a = b

class C2(object):
* * def __init__(self, a, b):
* * * * self.a = a
* * * * self.a = b

def main(N, test):
* * t0 = clock()

* * if test == 1:
* * * * [C1(ab, ab) for ab in xrange(N)]
* * elif test == 2:
* * * * [C2(ab, ab) for ab in xrange(N)]

* * t1 = clock()
* * print round(t1 - t0, 2)

main(N=700*1000, test=1)

Core duo 2 GHz:
test=1 ==1.06 s
test=2 ==3.0 s

(700*1000 is the way I have found to write the 700_000 I was talking
about, until we'll have a syntax for it.)

Bye,
bearophile
I did not expect such a large difference in instantiation time.
However I was thinking about
access time, and then the difference is not impressive (~20-25%):

from timeit import default_timer as clock

class C1(object):
__slots__ = ["a", "b"]
def __init__(self, a, b):
self.a = a
self.b = b

class C2(object):
def __init__(self, a, b):
self.a = a
self.b = b

def main(N, test):
t0 = clock()
if test == 'with slots':
c = C1(1, 2)
for _ in xrange(N):
(c.a, c.b)
elif test == 'without slots':
c = C2(1, 2)
for _ in xrange(N):
(c.a, c.b)
t1 = clock()
print test, round(t1 - t0, 3)

main(N=700*1000, test='with slots') # 0.152s
main(N=700*1000, test='without slots') # 0.195s

I mantain that one should use slots only as last resort, if the
speedup really justify having nonstandard classes.
Nov 4 '08 #30

P: n/a
Marc 'BlackJack' Rintsch <bj****@gmx.netwrites:
On Mon, 03 Nov 2008 23:32:25 +0000, Paulo J. Matos wrote:
>What's then the reason for adding named tuples if they are not
mutable...???

Names are more descriptive than "magic numbers" as indices. See for
example the "named tuple" returned by `os.stat()`.

Ciao,
Marc 'BlackJack' Rintsch
I'm reminded of a 'struct' decorator I made some time ago:

def struct(f):
classname = f.__name__
prop_names = f.func_code.co_varnames[:f.func_code.co_argcount]
def _new(cls, *args, **kwargs):
return tuple.__new__(cls, f(*args, **kwargs))
def _repr(self):
return '%s%s' % (type(self).__name__, tuple(self))
def prop(i):
return property(lambda self: self[i])
attrs = { '__slots__': (), '__new__': _new, '__repr__': _repr }
attrs.update((name, prop(i)) for i, name in enumerate(prop_names))
return type(classname, (tuple,), attrs)
The advantage over namedtuple is that you don't have to repeat the name
of the structure and you can easily define default values. The drawback
is that it involved some mild hackery (It won't work as is in Python 3
as f.func_code changed to f.__code__). It worked like this:
>>@struct
.... def Point(x=0, y=0): return float(x), float(y)
....
>>p, q = Point(2, 7), Point(y=3)
p, q
(Point(2.0, 7.0), Point(0.0, 3.0))
>>x, y = p
x, y
(2.0, 7.0)
>>p.x + p.y
9.0
>>p.x = 3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: can't set attribute
>>isinstance(p, tuple)
True

It's immutable but it would be easy to make it mutable by making it
inherit from list instead of tuple.

You could even have some processing:
>>@struct
.... def Interval(start, end, size=None):
.... size = end - start
.... return start, end, size
....
>>I = Interval(2.5, 6.0)
I.start, I.end, I.size
(2.5, 6.0, 3.5)

--
Arnaud
Nov 4 '08 #31

P: n/a
Michele Simionato <mi***************@gmail.comwrote:
I did not expect such a large difference in instantiation time.
However I was thinking about
access time, and then the difference is not impressive (~20-25%):
The difference in time is because when you create a normal instance Python
has to create at least two objects: the instance itself, and a dict for its
attributes. If you use __slots__ for all of the attributes then you no
longer have a separate __dict__ so you save that extra object creation.

As soon as you allow other attributes (which happens for example if you
have a base class without __slots__) I think you'll find the object
creation time goes back up to normal.

--
Duncan Booth http://kupuguy.blogspot.com
Nov 4 '08 #32

P: n/a
On Nov 3, 10:26*pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.auwrote:
On Tue, 04 Nov 2008 00:19:16 +0000, Marc 'BlackJack' Rintsch wrote:
On Mon, 03 Nov 2008 23:32:25 +0000, Paulo J. Matos wrote:
What's then the reason for adding named tuples if they are not
mutable...???
Names are more descriptive than "magic numbers" as indices. *See for
example the "named tuple" returned by `os.stat()`.

I have no objection to named tuples, but I've sometimes
missed having an equivalent to the Pascal record or C
struct: essentially a named mutable tuple.
+1. FWIW, I posted a recipe along these lines, following closely the
functionality and implementation of namedtuple: http://code.activestate.com/recipes/576555/

George
Nov 4 '08 #33

This discussion thread is closed

Replies have been disabled for this discussion.