Why does Dynamic Typing really matter?!?

Laura Creighton lac at strakt.com
Thu Feb 6 06:10:15 EST 2003


> Hi All
> 
> I'm doing some research into language constructs, particularly dynamic
> typing.  What I'm looking for are examples in dynamically typed
> languages that clearly illustrate the benefits that dynamic typing
> give to a solution to a problem.  I'm aware that dynamic typing
> provides the following,
> 
> 
> But what I want need is an solution to a problem that would not be
> possible to replicate in a statically typed language...
> 
> Can anyone pt me in the right direction or provide a small example?
> 
> Thanks
> Jason.
> -- 
> http://mail.python.org/mailman/listinfo/python-list

Get a copy of _Design Patterns_ and then a copy of _Design Patterns
Smalltalk Companion_.  Walk through the patterns one by one.  Many
(I think more than half)  of the Smalltalk ones discuss why theirs
is simpler than the C++ one due to the fact that Smalltalk is dynamially
typed.  For a final cut, get Pattern Hatching, where you can watch
John Vlissides finally decide that something was 'not a pattern' because
it was too easy to do in Smalltalk :-)

But if it is Python code you wanted ...

I wrote this a while ago (hmm, quite a while since I needed to
import nested_scopes) when somebody wanted to see the Decorator
Pattern.  Boy is it simple in Python ....

from __future__ import nested_scopes
import Tkinter

class WrappedWidget:
    def __init__(self, frame, widgetType=Tkinter.Label, **kw):
        self._widget=widgetType(frame, **kw)
        self.bind("<ButtonPress-1>", self.gotPressed)
        
    def __getattr__(self, name):
        if name.startswith("__") and name.endswith("__"):
            raise AttributeError, name
        else:
            return getattr(self._widget, name)

    def gotPressed(self, event):
        print 'I got Pressed!'
        self.configure(bg='red')

###########################  
if __name__ == '__main__':

    root = Tkinter.Tk()
    quit = Tkinter.Button(root, text="QUIT", fg="red", command=root.quit)
    quit.pack(side='left')

    Lab=WrappedWidget(root, fg='green', text='Label (default)')
    Lab.pack(side='left', fill='both', expand = 1)

    mydict={'fg':'yellow', 'text':'Button'}
    But=WrappedWidget(root, Tkinter.Button, **mydict)
    But.pack(side='left', fill='both', expand = 1)
    root.mainloop()
--------------------------------------------






More information about the Python-list mailing list