[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

ways to declare empty set variable

hyena

2/12/2008 1:46:00 PM

Maybe this is a very primative question, but I just get a bit confused about
'set' and 'Set' module in python.

I understand 'set' is a build in type in python after 2.4(or 2.3) and Set a
seperate module, anyhow, I gonna use build in 'set'.

then the question is how can I declare a empty set variable as a 'var= []'
do to a list variable?



27 Answers

Marc 'BlackJack' Rintsch

2/12/2008 1:57:00 PM

0

On Tue, 12 Feb 2008 14:45:43 +0100, Sun wrote:

> then the question is how can I declare a empty set variable as a 'var= []'
> do to a list variable?

You don't declare variables in Python. Just create an instance of `set`
and bind it to a name:

var = set()

Ciao,
Marc 'BlackJack' Rintsch

Chris

2/12/2008 1:57:00 PM

0

On Feb 12, 3:45 pm, "Sun" <a...@hut.at> wrote:
> Maybe this is a very primative question, but I just get a bit confused about
> 'set' and 'Set' module in python.
>
> I understand 'set' is a build in type in python after 2.4(or 2.3) and Set a
> seperate module, anyhow, I gonna use build in 'set'.
>
> then the question is how can I declare a empty set variable as a 'var= []'
> do to a list variable?

>>> test = set()
>>> test
set([])
>>> type(test)
<type 'set'>

You looking for that ?

hyena

2/12/2008 2:05:00 PM

0


"Chris" <cwitts@gmail.com> wrote in message
news:f30e545f-4485-4c0a-a9b2-476939818bc6@y5g2000hsf.googlegroups.com...
> On Feb 12, 3:45 pm, "Sun" <a...@hut.at> wrote:
>> Maybe this is a very primative question, but I just get a bit confused
>> about
>> 'set' and 'Set' module in python.
>>
>> I understand 'set' is a build in type in python after 2.4(or 2.3) and Set
>> a
>> seperate module, anyhow, I gonna use build in 'set'.
>>
>> then the question is how can I declare a empty set variable as a 'var=
>> []'
>> do to a list variable?
>
>>>> test = set()
>>>> test
> set([])
>>>> type(test)
> <type 'set'>
>
> You looking for that ?

yeah, that 's what I am looking for, thanks all for such prompt answers!

I was wondering why can't I use a format as "var = {} " to "var=list()" in
set variable, and decided not to bother with it.

Thanks.




Paul Rubin

2/12/2008 9:26:00 PM

0

"Sun" <as@hut.at> writes:
> I was wondering why can't I use a format as "var = {} " to "var=list()" in
> set variable, and decided not to bother with it.

In 3.0 you may be able to say {,} but there is a contingent that would
just as soon get rid of all that special syntax, so you'd say list()
instead of [], dict() instead of {}, etc.

Gabriel Genellina

2/12/2008 9:30:00 PM

0

En Tue, 12 Feb 2008 12:04:43 -0200, Sun <as@hut.at> escribió:
> "Chris" <cwitts@gmail.com> wrote

>>>>> test = set()
>>>>> test
>> set([])
>
> yeah, that 's what I am looking for, thanks all for such prompt answers!
>
> I was wondering why can't I use a format as "var = {} " to "var=list()"
> in
> set variable, and decided not to bother with it.

Python 3.0 has set literals {1,2,3} (perhaps they become frozensets
instead). But {} still is, and will be, an empty dict.
In reply to the n-th proposal to define a literal for empty sets, Guido
van Rossum said, in python-ideas:

"All possible proposals have already been discussed at length. Really,
writing set() isn't so bad. Get used to it."

http://mail.python.org/pipermail/python-ideas/2008-January/0...

--
Gabriel Genellina

Ben Finney

2/12/2008 9:35:00 PM

0

"Sun" <as@hut.at> writes:

> I was wondering why can't I use a format as "var = {} " to
> "var=list()" in set variable, and decided not to bother with it.

Python 3.0 will gain syntax to specify a literal of type 'set'
<URL:http://www.python.org/dev/peps/pep...::

>>> {17, "foo", 12.5}
set([17, 'foo', 12.5])

but this won't allow you to declare an empty set literal, because
that's already easy with 'set()', and '{}' is taken already for an
empty dict literal.

--
\ â??Unix is an operating system, OS/2 is half an operating system, |
`\ Windows is a shell, and DOS is a boot partition virus.� |
_o__) â??Peter H. Coffin |
Ben Finney

Bearophile

2/12/2008 10:47:00 PM

0

Paul Rubin:
> In 3.0 you may be able to say {,} but there is a contingent that would
> just as soon get rid of all that special syntax, so you'd say list()
> instead of [], dict() instead of {}, etc.

For Python 3.0 I'd like {} for the empty set and {:} for the empty
dict, but that idea was refused time ago, probably for some mental
backward compatibility. Missing that, I think dict() and set() and
tuple() and list() look better than using {} for the empty dict and
{/} for the empty set and () for empty tuple (or {} for the empty dict
and set() for the empty set).
dict() is a bit more verbose than {}, but it doesn't matter much. With
those dict(), set(), tuple(), list() the only little wart left is the
unary tuple literal: x, that I don't like much, maybe I'd like tuple
to be identified by a pair of delimiters, maybe like [|x|] or
something like that as in the Fortress language. I don't know...

Bye,
bearophile

Ben Finney

2/12/2008 11:05:00 PM

0

bearophileHUGS@lycos.com writes:

> For Python 3.0 I'd like {} for the empty set and {:} for the empty
> dict, but that idea was refused time ago, probably for some mental
> backward compatibility.

I agree with not breaking that backward compatibility; it seems
wanton.

> Missing that, I think dict() and set() and tuple() and list()

I often use these myself. They're slightly more explicit, which can
help when I want the reader not to have to think too much, and they're
not particularly verbose because the names are well-chosen and short.

> look better than using {} for the empty dict and {/} for the empty
> set and () for empty tuple

Note that '()' is syntactically null. Parentheses don't declare a
tuple literal, commas do. Parentheses are for grouping within
expressions, not specifying type.

> (or {} for the empty dict and set() for the empty set).

I thought you said above that you preferred 'set()' for an empty set?
I'm not sure what it is you're saying now.

> the only little wart left is the unary tuple literal: x, that I
> don't like much

I agree that it's a wart, but I think the harm done by trying to
change it would be more than the harm done by leaving it in.

--
\ â??[W]e are still the first generation of users, and for all that |
`\ we may have invented the net, we still donâ??t really get it.â? |
_o__) â??Douglas Adams |
Ben Finney

Bearophile

2/12/2008 11:49:00 PM

0

Ben Finney:
> I often use these myself. They're slightly more explicit, which can
> help when I want the reader not to have to think too much, and they're
> not particularly verbose because the names are well-chosen and short.

I'd like "list" be called "array" ;-)


> Note that '()' is syntactically null. Parentheses don't declare a
> tuple literal, commas do.

() is the literal for the empty tuple:

>>> t = ()
>>> type(t)
<type 'tuple'>
>>> (1, 2)[0:0]
()


>Parentheses are for grouping within expressions, not specifying type.<

I know, but I prefer Fortress in that regard, where each container has
its specific delimiter(s). In Python ( ) denote:
- expression grouping
- they are very often used to denote tuples (despite being necessary
only for the empty one)
- generators (x for x in ...).
The Boo language shows that () aren't that necessary for the
generators.


> I thought you said above that you preferred 'set()' for an empty set?
> I'm not sure what it is you're saying now.

Your language isn't my first one, and for me sometimes it's not easy
to express complex things :-) I can try again. Here are syntax pairs
(for empty dict && empty set) sorted from the (IMHO) the best one to
the worst one:
{:} && {}
dict() && set()
{} && set()
{} && {/}


> I think the harm done by trying to change it would be more
> than the harm done by leaving it in.

I agree.

Bye,
bearophile

Steve Holden

2/12/2008 11:51:00 PM

0

Ben Finney wrote:
[...]
>
> Note that '()' is syntactically null. Parentheses don't declare a
> tuple literal, commas do. Parentheses are for grouping within
> expressions, not specifying type.
>
Tell that to the interpreter:

>>> type(())
<type 'tuple'>
>>> tuple() is ()
True
>>>

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.hold...