From: Karsten Wutzke on
Hello,

this is obviously a Python OO question:

Since Python isn't stringly typed, single-dispatch isn't available per
se. So is the "double-dispatch" Visitor pattern, which is usually used
in OO systems to implement code generators. So, what is the de facto
method in Python to handle source code generation?

Karsten
From: Thomas Jollans on
On 07/15/2010 07:58 PM, Karsten Wutzke wrote:
> Hello,
>
> this is obviously a Python OO question:
>
> Since Python isn't stringly typed,

I expect this is an innocent typo, and you mean strictly.

> single-dispatch isn't available per se. So is the "double-dispatch" Visitor pattern,

Wait, what?
First of all, python is strictly typed in that every object has exactly
one type, which is different from other types. So you can't do "1"+2, as
you can in some other languages.

Anyway, this is interesting: Tell me more about how Python's dynamic
nature makes it impossible to do whatever you're trying to do. I'm
baffled. What are you trying to do, anyway?

> which is usually used
> in OO systems to implement code generators. So, what is the de facto
> method in Python to handle source code generation?

WHOA! Now if that isn't a Gedankensprung. Also, I'm still very far from
your train of thought, apparently: Now, the thing that code generators
probably share is that they write code to files. It depends on what I'm
trying to do of course, but I expect there's a good chance that if I
wrote a code generator in Python, it wouldn't be particularly
object-oriented at all.

From: Karsten Wutzke on
On 15 Jul., 20:28, Thomas Jollans <tho...(a)jollans.com> wrote:
> On 07/15/2010 07:58 PM, Karsten Wutzke wrote:
>
> > Hello,
>
> > this is obviously a Python OO question:
>
> > Since Python isn't stringly typed,
>
> I expect this is an innocent typo, and you mean strictly.
>
> > single-dispatch isn't available per se. So is the "double-dispatch" Visitor pattern,
>

Yes, typo, I meant strictly.

> Wait, what?
> First of all, python is strictly typed in that every object has exactly
> one type, which is different from other types. So you can't do "1"+2, as
> you can in some other languages.
>
> Anyway, this is interesting: Tell me more about how Python's dynamic
> nature makes it impossible to do whatever you're trying to do. I'm
> baffled. What are you trying to do, anyway?
>
> > which is usually used
> > in OO systems to implement code generators. So, what is the de facto
> > method in Python to handle source code generation?
>
> WHOA! Now if that isn't a Gedankensprung. Also, I'm still very far from
> your train of thought, apparently: Now, the thing that code generators
> probably share is that they write code to files. It depends on what I'm
> trying to do of course, but I expect there's a good chance that if I
> wrote a code generator in Python, it wouldn't be particularly
> object-oriented at all.

Well, I'm most experienced in OO, so writing OO in Python seems like
the way to start with Python. The visitor pattern uses single-
dispatch, that is, it determines which method to call be the type of
object passed in. I did some reading and it turned out that Python
can't do it without some tricks (function decorators and 3rd party
code). For what I'm doing, I can't, or rather don't want to rely on
3rd party code (that has reasons). Thus, the visitor OO pattern must
be replaced by some other way.

As I expected, you already hinted a non-OO solution. Which is now that
*I* am wondering what that would look like...

Note, that I have an hierarchical object structure which I want to
iterate over, so using OO looked natural to me. If there's a better
approach, I'm all ears.

Karsten
From: Christian Heimes on
> Since Python isn't stringly typed, single-dispatch isn't available per
> se. So is the "double-dispatch" Visitor pattern, which is usually used
> in OO systems to implement code generators. So, what is the de facto
> method in Python to handle source code generation?

Do you mean strongly typed langauge?

You are wrong, Python is a strongly typed language. Perhaps you are
confusing strong/weak with dynamic/static? These attributes are
orthogonal to each other. Python is a strongly and dynamicly typed language.

The visitor pattern is required for double dispatching in *some* OO
language like C++, to work around issues with inheritance and function
overloading. Python's OO works differently.

Christian

From: Karsten Wutzke on
>
> Yes, typo, I meant strictly.
>

Damn, I mean strongly. At least not for identifying which methods to
call depending on the type/s.

Karsten