Are there 'Interfaces' in Python??

Lee Morgan unknown at lee-morgan.net
Wed Sep 26 19:46:32 EDT 2001


Carl Banks <idot at vt.edu> writes:
> 
> 
> Now, if you want intefaces so as to catch errors when your classes
> fail to implement required methods, Python doesn't have anything like
> that.  But you can define a function that explicitly checks whether
> the class has the methods you require:
> 
>     def assert_provides_interface (klass):
>         assert has_attr (klass, 'methodX')
>         assert type(klass.methodX) == MethodType
>         ...
> 
>     class Y:
> 	def methodX ():
>             ...
> 
>     assert_provides_interface (Y)
> 
> 

A previous poster also suggested mutiple inheritance that raises errors when
methods aren't implemented. That implementation mean you get errors only at
runtime when the method is called.

I'd like to see interfaces to make this error checking explicit at byte-compile
time. 

As you suggest, due to Python's easy introspective behaviour you can implement
ways to do this yourself, but wouldn't an Interface keyword handle this alot
more cleanly - retaining the philosophy there's only one right way to do it.

I'd like to see an Interface implementation, even if it only really provides
no-brainer errors when refactoring code.

Cheers

-- 
Lee Morgan



More information about the Python-list mailing list