So I was getting around to using it when I realised that I cannot make
my class as abstract as can be. Here is my dilemma / requirements:
1. To create a YajlContentHandler class that forces all sub-classers
to implement a certain set of methods. (Great, thats what ABC is for)
There is a certain set of mutually exclusive callbacks, i.e. if you
implement the first set you need not implement the second, and vice
versa, so my second requirement is:
2. Conditional Abstractness! if certain methods are not implemented
then be able to require some method to be implemented.
Python is more flexible than Java, so having Conditional Meta Abstract
Base Classes seems only natural :P, maybe someone should write a PEP.
This only reminds me of the following tweet:
http://twitter.com/bos31337/status/13349058839
--
Hatem Nassrat
> 1. To create a YajlContentHandler class that forces all sub-classers
> to implement a certain set of methods. (Great, thats what ABC is for)
>
> There is a certain set of mutually exclusive callbacks, i.e. if you
> implement the first set you need not implement the second, and vice
> versa, so my second requirement is:
>
> 2. Conditional Abstractness! if certain methods are not implemented
> then be able to require some method to be implemented.
Mmm, can't you use two separate ABCs? Perhaps inheriting from a common
base.
--
Gabriel Genellina
> 1. To create a YajlContentHandler class that forces all sub-classers
> to implement a certain set of methods. (Great, thats what ABC is for)
>
> 2. Conditional Abstractness! if certain methods are not implemented
> then be able to require some method to be implemented.
You’re looking at it wrong. If you want to force people to do things in a
certain way, use Java. Python is about enabling things, not forcing them.
Don’t use subclassing. Instead, let the caller pass you a duck-typed object
that implements the methods you need.
LoL :-)
> Don’t use subclassing. Instead, let the caller pass you a duck-typed object
> that implements the methods you need.
I totally agree. in my code I do not force the object to be an
instance of a subclass of the ABC, they certainly can duck-type as
they please. The ABC is simply there to give them an idea of what
methods they need to implement. It is kind of self documenting
structure if you may. However, I had this problem described earlier
which does not let me complete this self documenting structure without
adding a thorough doc-string explaining that they still need to
implement one or two more methods.