Community Page
- blog.dhananjaynene.com Jump to website »
-
Subscribe -
Community
-
Top Commenters
-
Popular Threads
-
Recent Comments
- I hope you people realize how inefficient the code you've written is... After you've divided out all the 2's from the number, you still try to find out if it is divisible by...
- Boss!! and Bossess!!!! Please use perl for everything. Perl is ultimate for any purpose. Python is slow (even with psycho, pypy) and Java is over verbose. (But Java is faster) Check modperl before...
- Yes, and I needed to know. What is the smallest prime factor? I guessed around: 7 (Also, your "copy to clipboard" links don't work for me).
- There are supposed to be tab indentations. Not sure how to keep the tabs in a post
- I just wrote this code (works in python 3.0 with the special "//" for division of integers). This code is much faster and will find your prime factors. I take advantage of the fact that...
/var/log/mind
Dhananjay Nene’s free (as in free speech) opinions on all things related to Software EngineeringCommentary on Python from a Java programming perspective - Part 2 - How duck typing influences class design and design principles
Started by Dhananjay Nene · 9 months ago
This post talks about applying Open Closed Principle, Liskov’s Substitution Principle, Dependency Inversion Principle and Interface Segregation Principle in Python, coming from a Java programming background.
Background :
A few days ago I blogged about Commentary on Pyt ... Continue reading »
Background :
A few days ago I blogged about Commentary on Pyt ... Continue reading »
9 months ago
I think you can further merge the Button class and ButtonImpl class together in the Python example in the Dependency Inversion Principle (DIP) section?
In Python I very rarely see the suffix "Impl" appended to a class name. Since all classes in Python by nature are implementation classes, it makes sense to just leave "Impl" out of the name since it's redundant. The exception to this is existing extensions to the Python language which add "Interfaces" and re-use the class keyword to implement the interface concept out of necessity. In which case there is the strong convention to prefix an Interface class name with a capital I.
The phrasing of "TripBooker which would in turn call interface methods on other interfaces" is a bit fuzzy, since interfaces are only specifications and you can't actually "call interface methods" but instead something like "call methods on an object that provides an implementation of an interface".
You end the dependency inversion principle with, "In terms of the definition of DIP itself - abstractions no longer necessarily have to be implemented. They exist implicitly in the detail classes but are no longer explicitly documented." Python has a saying, "Explicit is better than Implicit", but in cases such as your Python dependency inversion example, the contract between what an object ("self") depends upon and the external object ("self.client") is highly implicit. You can only deduce the interface required by this dependency by inspecting the entire Button class and looking for all uses of "self.client". This is a common critique against using Python in a "large team" project, but there has been interesting work done in allowing you to make this dependency explicit with the Zope Component Architecture (ZCA). The Zope Component Architecture provides an Interface package (zope.interface) which lets you specify a contract (such as "IButtonClient has methods on() and off()") and then make explicit the fact that a class such as Button requires the "self.client" object to provide the IButtonClient interface. In cases where one object depends upon only a single other external object, there it's a common convention in Python to call that external object "context" (so it would be referred to as "self.context").
Zope 3 is a framework that builds upon the Zope Component Architecture, and XML is used to specify the dependencies between components (e.g. configuration and code are kept separate). There are also many who find developing with this strict separation tedious, especially in cases of agile or prototype-based development. In this case the practice of "convention over configuration" has been applied to the configuration aspects, and the configuration is deduced by using Python's powerful introspection capabilities - this technique is called "grokking" and is implemented in the Grok web framework - of which I am a happy Grok user :)
http://wiki.zope.org/zope3/Zope3Wiki
http://grok.zope.org
9 months ago
I did a minor restructuring of the words around DIP TripBooker as well. Thanks for pointing out.
I wasn't aware of explicit contracts using zope.interface and Zope Component Architecture. Definitely something I shall checkout.
9 months ago
for shape in shapes:
shape.draw()
9 months ago
9 months ago
shapes = [Circle(1,1,5), Square(2,4,3), Circle(3,3,7), Square(4,2,4)]
shapes.sort(key=lambda s: order.index(s.__class__))
for shape in shapes:
shape.draw()
9 months ago
9 months ago
9 months ago
The successive answer of mine refers to the Abstract Base Classes of Python3, that are absent from Python2.5, and that may interest you, so you may take a look at them:
http://www.python.org/dev/peps/pep-3119/
9 months ago
http://www.digitalmars.com/webnews/newsgroups.p...
9 months ago
9 months ago
That would be great. I would be pleased with the reference.
Thanks
Dhananjay
9 months ago