Movatterモバイル変換
[0]ホーム
asserting types
Alex Martellialeaxit at yahoo.com
Sat Apr 21 03:40:41 EDT 2001
"Paul Prescod" <paulp at ActiveState.com> wrote in messagenews:mailman.987815312.13601.python-list at python.org...> Mick wrote:> >> > What is the cleanest way to assert a class type? for example if one hasaassert isinstance(obj,cls)orassert issubclass(cls,super)depending on what you mean (a class object, or an instance object?). Butas everybody's observing, they're normally best not used.> > function that uses a particular class object then what is the nicest wayto> > assert within the function that the class is of the correct type? Iassume> > this is simple, so I guess more the question is this the wrong way tothink?> > should I be trying to organise things in a different way so as togenerate> > "compile" time errors?>> It is generally considered bad form to try to assert a particular class.> In Python it is more profitable to presume that the users of your module> know what they are doing. If they give you a class that is not a> subclass of yours, they may do it for a good reason (i.e. it is a> C-module for speed, or it is an object from another module that> implements the right interface).Right! See for examplehttp://www.activestate.com/ASPN/Python/Cookbook/Recipe/52295for a nice useful idiom that gets destroyed if client code uses suchassertions -- and a resulting impassioned plea not to use typetests:-).> If you want to do a sanity check, the best thing is to check that the> methods you expect to be available are available. Even just one or two> suffices as a sanity check. If they implement those one or two but not> other important ones then again, they may feel that they know what they> are doing (i.e. a particular code path is known not to call the other> methods) and you should just trust them.Generally, just trying all the operations you need is OK -- your codemay or may not need to trap exeptions that may result (it may leavethat task to the caller). Sometimes you do need to make sure thatcertain state alterations are not *partially* performed -- if some ofthem are done, all must be; being passed an object that hase somebut not all of the needed methods may then be pernicious. If thestate being altered is under your control, you might save a stateportion, do the work in a try/except, restore the state saved if theoperation didn't fully work out. Problem is stickier when state isbeing altered in objects you don't fully control, so saving/restoringstate is not an option.For those cases, only, accurate checking may be worthwhile, buteven then it's rarely going to be a type-check (only, perhaps, asa simpler and known to be inaccurate stand-in for checking whatyou really need to know; you could for example use it for a first'fast-path' check -- "if the object isinstance of X I'm surely fine,else let's check if it does have what I really specifically need").http://www.activestate.com/ASPN/Python/Cookbook/Recipe/52291gives an example of "accurate checking" -- the discussion thereneeds to be expanded, but even as it stands it may help putthings in this perspective.Alex
More information about the Python-listmailing list
[8]ページ先頭