How do I process True only when the object is a list or tuple, and False when the string is false?

Asked 1 years ago, Updated 1 years ago, 98 views

If an object is a list/tuple, you want to continue to run the program, and if it is a string, you want to create a script that says assert.

assert isinstance(lst, list) or isinstance(lst,tuple) Is there a better way than sharing it?

list python assert type

2022-09-22 22:24

1 Answers

First, the second factor of isinstance() can be tuple-type assert isinstance(lst, list) or isinstance(lst, tuple) can be reduced to assert is instance(lst, (list, tuple)).

And when you use the ASSERT, I think assert not isinstance(lst, basestring) I think it's a better way to use it (Please change to string since Python 3 cannot use basestring)

If you write it like the way you asked, Although it is not subclass in list/tuple, assertionError occurs in other types that behave like list/tuple.


2022-09-22 22:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.