Aby odpowiedzieć na pierwsze pytanie, dlaczego lista pustych wartości nie jest uważany za pusty, to dlatego, że nie zawierają coś, nawet jeśli te rzeczy są same puste. Pomyśl o tym jak o pudełku pustych pudełek.
Poniższy kod pokazuje jeden ze sposobów modyfikacji funkcji foo()
, aby zrobić to, co chcesz (i przetestować). Twoje wyobrażenie o tym, czym jest pusta lista, było zaskakująco trudne, częściowo dlatego, że jest sprzeczne z tym, co sam język uważa za puste.Jak widać cała logika zajmująca się ustalaniem, czy lista jest "pusta" zgodnie z twoją definicją została przeniesiona do oddzielnej funkcji o nazwie empty_list()
, ponieważ prawdopodobnie niewiele ma wspólnego z resztą tego, co musi osiągnąć foo()
. To nie jest zbyt skomplikowane i jeśli nic innego nie powinno dostarczyć dobrego punktu wyjścia.
Ponadto, nie powiedziałeś, co powinien zrobić, jeśli argument, który przekazał, nie jest listą żadnego rodzaju, ani listą, ale nie zawierał innych list ani ciągów znaków, więc napisanie go spowoduje podniesienie TypeError
wyjątek - coś, co jest podobne do sposobu, w jaki większość wbudowanych funkcji w Pythonie reaguje, gdy tak się dzieje. Poniżej znajduje się przykładowy kod, a jego produkcja Test:
try:
string_type = basestring
except NameError: # probably Python 3.x
string_type = str
class _NULL(object): # unique marker object
def __repr__(self): return '<nothing>'
_NULL = _NULL()
def empty_list(arg=_NULL):
arg = arg if arg is not _NULL else []
if not isinstance(arg, (list, string_type)):
raise TypeError
elif isinstance(arg, string_type):
return not len(arg)
else:
return len(arg) == 0 or all(empty_list(e) for e in arg)
def foo(list_=None):
if list_ is None or empty_list(list_):
print 'list is empty'
else:
print 'list is not empty'
testcases = [
_NULL,
[],
[''],
['', ''],
['', ['']],
['abc'],
['', 'abc'],
[False],
[None],
[0],
[0.0],
[0L],
[0j],
[42],
[{}],
[{'':0}],
[{'a':1}],
False,
None,
0,
0.0,
0L,
0j,
42,
{},
{'':0},
{'a':1},
]
for arg in testcases:
call = 'foo({!r:s}) ->'.format(arg)
print '{!s:>20s}'.format(call),
try:
foo() if arg is _NULL else foo(arg)
except TypeError:
print 'illegal argument exception'
tu jest wyjście produkuje z Python 2.7:
foo(<nothing>) -> list is empty
foo([]) -> list is empty
foo(['']) -> list is empty
foo(['', '']) -> list is empty
foo(['', ['']]) -> list is empty
foo(['abc']) -> list is not empty
foo(['', 'abc']) -> list is not empty
foo([False]) -> illegal argument exception
foo([None]) -> illegal argument exception
foo([0]) -> illegal argument exception
foo([0.0]) -> illegal argument exception
foo([0L]) -> illegal argument exception
foo([0j]) -> illegal argument exception
foo([42]) -> illegal argument exception
foo([{}]) -> illegal argument exception
foo([{'': 0}]) -> illegal argument exception
foo([{'a': 1}]) -> illegal argument exception
foo(False) -> illegal argument exception
foo(None) -> list is empty
foo(0) -> illegal argument exception
foo(0.0) -> illegal argument exception
foo(0L) -> illegal argument exception
foo(0j) -> illegal argument exception
foo(42) -> illegal argument exception
foo({}) -> illegal argument exception
foo({'': 0}) -> illegal argument exception
foo({'a': 1}) -> illegal argument exception
Uwaga- rozumiem w przypadku x = [ „”] lista nie jest naprawdę puste, ale wciąż wiesz, że jest puste :) (lub nie ma wartości/danych, których można użyć) –
Co tak naprawdę naprawdę robisz? Dlaczego ** chcesz, aby ** '['', '']] było" puste "? –
['', ''] nie jest dużym problemem .... dla tego zestawu (['', '']) może być użyty !! –