Structures

P

Paulo J. Matos

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,
 
B

bearophileHUGS

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
 
P

Paulo J. Matos

Paulo J. Matos said:
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!
 
J

Jorgen Grahn

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
 
A

Arnaud Delobelle

Ben Finney said:
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.

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

Aaron Brady

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_Hand_Grenade_of_Antioch#Usage_instructions
 
C

Craig Allen

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

Arnaud Delobelle

Craig Allen said:
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
 
P

Paulo J. Matos

Note that structs are mutable.

Ah... :( That's a no go!
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...???
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
 
P

Paulo J. Matos

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_Hand_Grenade_of_Antioch#Usage_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?]
 
J

Joe Strout

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
 
G

George Sakkis

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
 
M

Marc 'BlackJack' Rintsch

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
 
P

Paulo J. Matos

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

Paul Rubin

Paulo J. Matos said:
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.
 
G

George Sakkis

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__:
.... __slots__ = ('x', 'y')
....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
 
A

Aaron Brady

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

Aaron Brady

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

Glenn Linderman

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.
 

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
474,265
Messages
2,571,069
Members
48,771
Latest member
ElysaD

Latest Threads

Top