Newbie: static typing?

Rui Maciel rui.maciel at gmail.com
Tue Aug 6 06:07:56 EDT 2013


Joshua Landau wrote:

> Unless you have a very good reason, don't do this. It's a damn pain
> when functions won't accept my custom types with equivalent
> functionality -- Python's a duck-typed language and it should behave
> like one.

In that case what's the pythonic way to deal with standard cases like this 
one?

<code>
class SomeModel(object):
        def __init__(self):
                self.label = "this is a label attribute"

        def accept(self, visitor):
                visitor.visit(self)
                print("visited: ", self.label)


class AbstractVisitor(object):
        def visit(self, element):
                pass
    

class ConcreteVisitorA(AbstractVisitor):
        def visit(self, element):
                element.label = "ConcreteVisitorA operated on this model"

class ConcreteVisitorB(AbstractVisitor):
        def visit(self, element):
                element.label = "ConcreteVisitorB operated on this model"


model = SomeModel()

operatorA = ConcreteVisitorA()

model.accept(operatorA)

operatorB = ConcreteVisitorB()

model.accept(operatorA)

not_a_valid_type = "foo"

model.accept(not_a_valid_type)
</python>


Rui Maciel



More information about the Python-list mailing list