Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Stylistic question about inheritance (http://www.velocityreviews.com/forums/t343275-stylistic-question-about-inheritance.html)

Andrew Koenig 03-31-2005 07:33 PM

Stylistic question about inheritance
 
Suppose I want to define a class hierarchy that represents expressions, for
use in a compiler or something similar.

We might imagine various kinds of expressions, classified by their top-level
operator (if any). So, an expression might be a primary (which, in turn,
might be a variable or a constant), a unary expression (i.e. the result of
applying a unary operator to an expression), a binary expression, and so on.

If I were solving such a problem in C++, I would define a base class for all
expressions, then derive the various kinds of expression classes from that
base class. However, I would not anticipate ever creating objects of the
base class, so I would make it abstract.

In Python, I can imagine doing the same thing:

class Expr(object):
pass

class UnaryExpr(Expr):
# ...

class BinaryExpr(Expr):
# ...

and so on. However, although I don't have a choice in C++ about having a
base class--you can't use dynamic binding without it--in Python I do have
that choice. That is, I don't need to have the base class at all unless I
want to have some operations that are common to all derived classes.

Of course, there are reasons to have a base class anyway. For example, I
might want it so that type queries such as isinstance(foo, Expr) work. My
question is: Are there other reasons to create a base class when I don't
really need it right now?



Carl Banks 03-31-2005 08:15 PM

Re: Stylistic question about inheritance
 

Andrew Koenig wrote:
[snip]
> Of course, there are reasons to have a base class anyway. For

example, I
> might want it so that type queries such as isinstance(foo, Expr)

work. My
> question is: Are there other reasons to create a base class when I

don't
> really need it right now?


Well, Python seems to get along fine without the ability to do
isinstance(foo,file_like_object); probably better off in the end for
it. So I'd say you should generally not do it. Inheritence is for
when different classes need to share functionality.


--
CARL BANKS


=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?= 03-31-2005 08:18 PM

Re: Stylistic question about inheritance
 
Andrew Koenig wrote:
> Of course, there are reasons to have a base class anyway. For example, I
> might want it so that type queries such as isinstance(foo, Expr) work. My
> question is: Are there other reasons to create a base class when I don't
> really need it right now?


You would normally try to avoid type queries, and rely on virtual
methods instead, if possible. It seems likely for the application
that code can be shared across different subclasses, for example,
you might be able to define

def Expr:
def __str__(self):
return '%s(%s)' % (self.__class__.__name__,
", ".join(map(str, self.operands()))

requiring you only to implement .operands() in the subclasses.

If you can anticipate such common code, it is easier to add
a base class right away. If you cannot think of a specific
use case, there is little point in having a common base class.

Regards,
Martin

Lonnie Princehouse 03-31-2005 08:22 PM

Re: Stylistic question about inheritance
 
If you try this sort of inheritance, I'd recommend writing down the
formal grammar before you start writing classes. Don't try to define
the grammar through the inheritance hierarchy; it's too easy to
accidentally build a hierarchy that can't be translated into a
single-pass-parsable grammar...

I usually skip the inheritance and make everything an instance of the
same class, e.g.

class ASTNode(object): ...

class Stmt(ASTNode): ...
class Expr(ASTNode): ...
class UnaryExpr(ASTNode): ...
class BinaryExpr(ASTNode): ...

or you could dynamically generate classes with inheritance based on a
grammar definition


Andrew Koenig 03-31-2005 08:23 PM

Re: Stylistic question about inheritance
 
"Carl Banks" <invalidemail@aerojockey.com> wrote in message
news:1112300127.449931.146470@o13g2000cwo.googlegr oups.com...

> Well, Python seems to get along fine without the ability to do
> isinstance(foo,file_like_object); probably better off in the end for
> it. So I'd say you should generally not do it. Inheritence is for
> when different classes need to share functionality.


That's really the question: Is it for when they need to share
functionality, or when they are conceptually related in ways that might lead
to shared functionality later?



Andrew Koenig 03-31-2005 08:24 PM

Re: Stylistic question about inheritance
 
""Martin v. Löwis"" <martin@v.loewis.de> wrote in message
news:424C5B09.9090006@v.loewis.de...

> You would normally try to avoid type queries, and rely on virtual
> methods instead, if possible.


Of course.

> It seems likely for the application
> that code can be shared across different subclasses, for example,
> you might be able to define
>
> def Expr:
> def __str__(self):
> return '%s(%s)' % (self.__class__.__name__,
> ", ".join(map(str, self.operands()))
>
> requiring you only to implement .operands() in the subclasses.


Indeed.

> If you can anticipate such common code, it is easier to add
> a base class right away. If you cannot think of a specific
> use case, there is little point in having a common base class.


So, for example, you don't think it's worth including the base class as a
way of indicating future intent?



Andrew Koenig 03-31-2005 08:28 PM

Re: Stylistic question about inheritance
 
"Lonnie Princehouse" <finite.automaton@gmail.com> wrote in message
news:1112300578.456411.274110@f14g2000cwb.googlegr oups.com...

> If you try this sort of inheritance, I'd recommend writing down the
> formal grammar before you start writing classes. Don't try to define
> the grammar through the inheritance hierarchy; it's too easy to
> accidentally build a hierarchy that can't be translated into a
> single-pass-parsable grammar...


Understood. I was using expression trees as a contrived example, and really
want to know about the Python community's stylistic preferences for defing
such hierarchies that don't absolutely need a root.

> I usually skip the inheritance and make everything an instance of the
> same class, e.g.
>
> class ASTNode(object): ...
>
> class Stmt(ASTNode): ...
> class Expr(ASTNode): ...
> class UnaryExpr(ASTNode): ...
> class BinaryExpr(ASTNode): ...


Eh? There's still inheritance here: Everything is derived from ASTNode. I
understand that there is a separate design issue whether to make the
hierarchy deep or shallow, but it's still a hierarchy.




Irmen de Jong 03-31-2005 08:40 PM

Re: Stylistic question about inheritance
 
Andrew Koenig wrote:
> "Lonnie Princehouse" <finite.automaton@gmail.com> wrote in message
> news:1112300578.456411.274110@f14g2000cwb.googlegr oups.com...
>
>
>>If you try this sort of inheritance, I'd recommend writing down the
>>formal grammar before you start writing classes. Don't try to define
>>the grammar through the inheritance hierarchy; it's too easy to
>>accidentally build a hierarchy that can't be translated into a
>>single-pass-parsable grammar...

>
>
> Understood. I was using expression trees as a contrived example, and really
> want to know about the Python community's stylistic preferences for defing
> such hierarchies that don't absolutely need a root.


I have used empty or near-empty base classes to be some sort of
class 'tag' for the derived classes.
Much like Java's Serializable interface; it adds nothing on
a functional level but you can check if a class has a 'tag'
by checking if it is an instance of the base class.
I don't know if this is good style in Python but I tend
to use it sometimes (probably because I do Java at work ;-)

--Irmen

Stefan Seefeld 03-31-2005 08:49 PM

Re: Stylistic question about inheritance
 
Andrew Koenig wrote:

> Of course, there are reasons to have a base class anyway. For example, I
> might want it so that type queries such as isinstance(foo, Expr) work. My
> question is: Are there other reasons to create a base class when I don't
> really need it right now?


Coming from C++ myself, I still prefer to use inheritance even if Python
doesn't force me to do it. It's simply a matter of mapping the conceptual
model to the actual design/implementation, if ever possible.

Regards,
Stefan



Donn Cave 03-31-2005 08:55 PM

Re: Stylistic question about inheritance
 
In article
<P_Y2e.493283$w62.145022@bgtnsc05-news.ops.worldnet.att.net>,
"Andrew Koenig" <ark@acm.org> wrote:

> "Carl Banks" <invalidemail@aerojockey.com> wrote in message
> news:1112300127.449931.146470@o13g2000cwo.googlegr oups.com...
>
> > Well, Python seems to get along fine without the ability to do
> > isinstance(foo,file_like_object); probably better off in the end for
> > it. So I'd say you should generally not do it. Inheritence is for
> > when different classes need to share functionality.

>
> That's really the question: Is it for when they need to share
> functionality, or when they are conceptually related in ways that might lead
> to shared functionality later?


No -- inheritance is for implementation, not to express conceptual
relationship.

Donn Cave, donn@u.washington.edu


All times are GMT. The time now is 07:56 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57