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

PEP218: Representing the empty set

P: n/a
Looking at PEP218 there is a discussion about the most appropriate way
of representing the empty set. The two alternatives proposed are {} and
{-}. I was wondering why either of these is needed. Why not just use
set()?. It is only two more characters than {-} and a bit more explicit.

Similarly, I find myself initialising dictionaries with dict() rather
than {}.

Am I missing something?

--
Andrew McLean
Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Andrew McLean wrote:

Looking at PEP218 there is a discussion about the most appropriate way
of representing the empty set. The two alternatives proposed are {} and
{-}. I was wondering why either of these is needed. Why not just use
set()?. It is only two more characters than {-} and a bit more explicit.

Similarly, I find myself initialising dictionaries with dict() rather
than {}.

Am I missing something?


Maybe it's nothing more than this distinction: dict() is a call which
*returns* an empty dictionary. {} *is* an empty dictionary. Likewise,
set() returns an empty set, which presumably {-} directly *is* an empty
set.

As someone not involved in the participation, I don't know how much
value my observation has, but to me it looks like a question of readability.
The direct representation flows better than the function call/constructor
approach...

-Peter
Jul 18 '05 #2

P: n/a
Peter Hansen <pe***@engcorp.com> writes:
Maybe it's nothing more than this distinction: dict() is a call which
*returns* an empty dictionary. {} *is* an empty dictionary.


I don't understand in what ways these are really distinct.

a = []
for i in range(2):
a.append({})
print (a[0] is a[1])

prints 0, if that helps. Clearly "a = {}" and "a = dict()" both run
some code that creates a new empty dictionary. It's not the case that
"a = {}" sets a to an empty dict created at compile time or anything
like that.
Jul 18 '05 #3

P: n/a
Paul Rubin wrote:

Peter Hansen <pe***@engcorp.com> writes:
Maybe it's nothing more than this distinction: dict() is a call which
*returns* an empty dictionary. {} *is* an empty dictionary.


I don't understand in what ways these are really distinct.

a = []
for i in range(2):
a.append({})
print (a[0] is a[1])

prints 0, if that helps. Clearly "a = {}" and "a = dict()" both run
some code that creates a new empty dictionary. It's not the case that
"a = {}" sets a to an empty dict created at compile time or anything
like that.


I agree. In that example, either is equally readable.

I think the example I would have in mind is a comparison:

if a == []:
# do something
if b != {}:
a.append(b)

or whatever.

I think the difference lies in ones intent. If the empty item is
going to be kept around and used, then a constructor approach seems
appropriate, maybe even arguably more readable. If the empty item
is merely a temporary, to be used and discarded, then the apparent
extra overhead of the call, and the less succinct syntax, lead me to
think the short form is preferable.

-Peter
Jul 18 '05 #4

P: n/a
"Andrew McLean" <sp***********@at-andros.demon.co.uk> wrote in message
news:Ea**************@at-andros.demon.co.uk...
Looking at PEP218 there is a discussion about the most appropriate way
of representing the empty set. The two alternatives proposed are {} and
{-}. I was wondering why either of these is needed. Why not just use
set()?. It is only two more characters than {-} and a bit more explicit.

Similarly, I find myself initialising dictionaries with dict() rather
than {}.

Am I missing something?
A very minor oversight. This discussion only makes sense in
the context of having a set literal. The 2.3 set implementation
certainly didn't, and the latest 2.4 development documentation
(Feb 12) also doesn't, so the discussion is moot.

John Roth
--
Andrew McLean

Jul 18 '05 #5

P: n/a
Peter Hansen <pe***@engcorp.com> writes:
I think the example I would have in mind is a comparison:

if a == []:
# do something
if b != {}:
a.append(b)

or whatever.... If the empty item
is merely a temporary, to be used and discarded, then the apparent
extra overhead of the call, and the less succinct syntax, lead me to
think the short form is preferable.


But you suffer the overhead either way. The {} syntax may be worse
than the function call syntax because it looks like it avoids the
overhead when it really doesn't. The correct way to avoid the
overhead is with "if len(b) != 0: ... ".
Jul 18 '05 #6

P: n/a
Paul Rubin <http://ph****@NOSPAM.invalid> wrote:

But you suffer the overhead either way. The {} syntax may be worse
than the function call syntax because it looks like it avoids the
overhead when it really doesn't. The correct way to avoid the
overhead is with "if len(b) != 0: ... ".


Or, the way I would write it:

if b:
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.