Prev: Why the inconsistent of those two base64 methods?
Next: client to upload big files via https and get progress info
From: Hatem Nassrat on 12 May 2010 00:38 Today I was doing a major re-write of a library I called yajl-py that wraps the json 'sax-like' c-parser yajl, and decided I should look into absract base classes since I knew they had been added to py26. Truthfully, I was surprised when I found out that the BDFL accepted this PEP, but hey were in 2010 :p. 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
From: Gabriel Genellina on 12 May 2010 01:29 En Wed, 12 May 2010 01:38:47 -0300, Hatem Nassrat <hnassrat(a)gmail.com> escribi�: > 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
From: Lawrence D'Oliveiro on 13 May 2010 08:30 In message <mailman.90.1273639153.32709.python-list(a)python.org>, Hatem Nassrat wrote: > 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.
From: Pykler on 25 May 2010 23:41
On May 13, 9:30 am, Lawrence D'Oliveiro <l...(a)geek- central.gen.new_zealand> wrote: > Youre 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. LoL :-) > Dont 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. |