Generating a list of None

N

Nicolas Couture

Hi,

Is it possible to generate a list of `None' ?

opts.__dict__.values() below could be represented by [None, None, None]

---
def get_options(opts):
"""Return True or False if an option is set or not"""
vals = opts.__dict__.values()

for val in vals:
if val is not None:
return True

return False
---

This is how I'd like to see it:

---
def get_options(opts):
"""Return True or False if an option is set or not"""
vals = opts.__dict__.values()

for if vals is [None * len(vals)]:
return False

return True
---
 
N

Nicolas Couture

of course the later snipplet should be:
---
def get_options(opts):
"""Return True or False if an option is set or not"""
vals = opts.__dict__.values()

if vals == [None * len(vals)]:
return False

return True
---
 
B

Bengt Richter

Hi,

Is it possible to generate a list of `None' ?

opts.__dict__.values() below could be represented by [None, None, None]

---
def get_options(opts):
"""Return True or False if an option is set or not"""
vals = opts.__dict__.values()

for val in vals:
if val is not None:
return True

return False
---

This is how I'd like to see it:

---
def get_options(opts):
"""Return True or False if an option is set or not"""
vals = opts.__dict__.values()

for if vals is [None * len(vals)]:
return False

return True
---

how about (untested)

def get_options(opts):
"""Return True or False if an option is set or not"""
return [1 for val in vars(opts).values() if val is not None] and True or False

Regards,
Bengt Richter
 
R

Raymond Hettinger

[Bengt Richter]
how about (untested)

def get_options(opts):
"""Return True or False if an option is set or not"""
return [1 for val in vars(opts).values() if val is not None] and True or False

While we're tossing around hacks and coding atrocities, we should note
that:

not not x

outperforms:

x and True or False

neither of which is as clear as:

bool(x)


Raymond
 
B

Bengt Richter

[Bengt Richter]
how about (untested)

def get_options(opts):
"""Return True or False if an option is set or not"""
return [1 for val in vars(opts).values() if val is not None] and True or False

While we're tossing around hacks and coding atrocities, we should note
that:

not not x

outperforms:

x and True or False

neither of which is as clear as:

bool(x)
Point. ;-)
(I actually thought to do not not x and bool(x) also crossed my mind, so
I'm not sure why I wound up with the above).

BTW, I imagine this is probably faster (and closer to the OP's approach),
at least for large option sets: (maybe needs comment like # not all None's ? ;-)
... values = vars(opt).values()
... return values.count(None) != len(values)
... {'a': None, 'c': 'option c', 'b': None}

Regards,
Bengt Richter
 

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
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top