Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Python and subclassing - not necessarily like other languages
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  10 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Grant Paton-Simpson  
View profile  
 More options Jul 16 2012, 7:50 pm
From: Grant Paton-Simpson <gr...@p-s.co.nz>
Date: Tue, 17 Jul 2012 11:50:16 +1200
Local: Mon, Jul 16 2012 7:50 pm
Subject: Python and subclassing - not necessarily like other languages
http://pyvideo.org/video/879/the-art-of-subclassing

> All problems have simple, easy-to-understand, logical wrong answers.
> Subclassing in Python is no exception. Avoid the common pitfalls and
> learn everything you need to know about making effective use of
> inheritance in Python.

Interesting explanation of superclass (think from the point of view of
possible subclasses, not yourself).

Challenges to the notion that a subclass is simply a form of
specialisation. Better to think in terms of code reuse (the class that
has the most reusable code should be the parent).

Well worth watching.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Collins  
View profile  
 More options Jul 16 2012, 10:30 pm
From: Robert Collins <robe...@robertcollins.net>
Date: Tue, 17 Jul 2012 14:30:34 +1200
Local: Mon, Jul 16 2012 10:30 pm
Subject: Re: [nzpug] Python and subclassing - not necessarily like other languages

I haven't watched that one yet, but I'd challenge the idea of
subclassing for code reuse at all, its usually a mistake IME.

-Rob


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tim McNamara  
View profile  
 More options Jul 17 2012, 12:39 am
From: Tim McNamara <mcnamara....@gmail.com>
Date: Tue, 17 Jul 2012 16:39:30 +1200
Local: Tues, Jul 17 2012 12:39 am
Subject: Re: [nzpug] Python and subclassing - not necessarily like other languages
On 17 July 2012 14:30, Robert Collins <robe...@robertcollins.net> wrote:

Wow, powerful statement. Especially as ah.. that's arguably the point
of adding classes to a language. Otherwise they're just fancy
dictionaries.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Collins  
View profile  
 More options Jul 17 2012, 5:11 am
From: Robert Collins <robe...@robertcollins.net>
Date: Tue, 17 Jul 2012 21:11:04 +1200
Local: Tues, Jul 17 2012 5:11 am
Subject: Re: [nzpug] Python and subclassing - not necessarily like other languages

On Tue, Jul 17, 2012 at 4:39 PM, Tim McNamara <mcnamara....@gmail.com> wrote:

>> I haven't watched that one yet, but I'd challenge the idea of
>> subclassing for code reuse at all, its usually a mistake IME.

>> -Rob

> Wow, powerful statement. Especially as ah.. that's arguably the point
> of adding classes to a language. Otherwise they're just fancy
> dictionaries.

Thats also a debatable position (especially considering that some
recent languages omit classes entirely :)). I think its best answered
through a few things - other uses of classes than arranging for code
reuse, the lack of similarity to fancy dictionaries even in Python,
other more effective ways to reuse code and the downsides of using a
class hierarchy to achieve code reuse.

On the fancy dictionaries side, you might be referring to __dict__,
which is a special case: its not part of the object protocol -
instances of extension classes don't have it, and regular classes made
with __slots__ don't have it:

>>> class Foo(object):

...     __slots__ = ['a']
...
>>> Foo().__dict__

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Foo' object has no attribute '__dict__'
>>> class Bar(object):

...   pass
...
>>> Bar().__dict__

{}

You might also be referring to the way you can just assign attributes
and read them back: but this isn't object orientation so much as it is
plain old data storage: classes are an object orientation and data
hiding tool: acting on someone elses data can lead to very vague
contracts, and when you're programming a large system that really
really hurts. Or you might be referencing the way you end up with a
namespace to do things on - but this is rather specific to Python. If
you look at other languages like e.g. Smalltalk, C++, or Java (picking
three fairly different, object orientated languages), the namespace
for talking about data storage, and for passing messages can be
handled very differently: it can be totally freeform as it is (mostly)
in Python [see descriptors for where it is not], or it can be both
fixed and totally partitioned like in Smalltalk: you cannot
accidentally refer to a member variable vs a method, the syntax is
unambiguous (and there is perfect data hiding: outside the class code
only methods can be seen). Or it can be very flexible with fine
grained rules over who can see what, and when, like it is in C++ and
Java. Any which way you look at it, these things are much more than
dictionaries when you have language support determining how it can be
accessed and which keys can be seen by what callers.

What other uses are there for objects other than code reuse? Well..
classes provide objects, and objects are extremely useful:
 - in the absence of type based dispatch they give you polymorphism
without (developer managed) global state.
 - they provide data hiding to separate out the plumbing and the taps
 - and defensive barriers to ensure that invariants covering the
relationship between multiple atoms are preserved.

How else can we reuse code? There are a few fundamental ways: We can
compose objects - well known techniques like adapters are precisely
this. We can use traits (not available in Python, but available
elsewhere and -very- nice to work with). We can use functions [e.g.
where some required behaviour is not on an axis of freedom for
polymorphism, this prevents inappropriate variation vs using a
method]. Outside of class based languages we get into fun stuff like
prototypes, and thats a mind bending thing all of its own.

What about the downsides? Why should we treat subclassing as a last
resort not a first-chance tool? There are a few key reasons. The first
is that designing things to be subclassable is really rather hard:
subclasses get access to some of your classes internals (I know that
in Python this is more convention than anything :)) and so you need to
decide which things that are plumbing you trust third party code to
fiddle with safely. An argument can be made that this is waste: just
let subclasses do what they will and it will be fine: but when you've
got (say) 40 or 50 discrete code bases subclassing a class you wrote,
that exerts a fantastic evolutionary pressure to change it only
carefully. When that pressure is applied only to the public interface,
its a lot easier to refactor and improve the implementation without
gymnastics.

Secondly, there is the conflation of sub-type and sub-class; when you
subclass something to reuse its code, you are constrained to either a)
preserve its contract in total or b) not be a sub-type of the base
class. As a concrete example, the following two types have a subclass
relationship but not a subtype relationship.

>>> class Parent(object):

...     pass
...
>>> class Child(Parent):

...     def __init__(self, new_mandatory):
...         pass
...
>>> def uses_the_type(a_class):

...     return a_class()
...
>>> uses_the_type(Parent)

<__main__.Parent object at 0x1196ed0>
>>> uses_the_type(Child)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in uses_the_type
TypeError: __init__() takes exactly 2 arguments (1 given)

^ Note that the error given is entirely accurate: the subtype
relationship was not preserved - these two classes are not Liskov
substitutable.

Thirdly, its extremely common to find new and unrelated uses for code
as time goes by; if you are a fan of DRY, you will want to reuse them:
subclassing things without a sensible is-a relationship will tend to
break other code that uses isinstance(even though thats a bad smell),
violate guidelines like 'one concept per class' (because you end up
inheriting the concepts of the base class as well as the code), and
make you more vulnerable to action-at-a-distance bugs where changes in
the base class break other code without warning. Even when tests catch
that its still unpleasant, and keeping things tight, focused and
orthogonal is a good way to avoid it.

-Rob


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Francois Marier  
View profile  
 More options Jul 17 2012, 6:13 am
From: Francois Marier <franc...@fmarier.org>
Date: Tue, 17 Jul 2012 22:13:19 +1200
Local: Tues, Jul 17 2012 6:13 am
Subject: Re: [nzpug] Python and subclassing - not necessarily like other languages
While we are on the topic of heresy ;)

  http://harmful.cat-v.org/software/OO_programming/why_oo_sucks

Cheers,
Francois

--
Francois Marier           identi.ca/fmarier
http://fmarier.org      twitter.com/fmarier


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Grant Paton-Simpson  
View profile  
 More options Jul 17 2012, 8:08 am
From: Grant Paton-Simpson <gr...@p-s.co.nz>
Date: Wed, 18 Jul 2012 00:08:48 +1200
Local: Tues, Jul 17 2012 8:08 am
Subject: Re: [nzpug] Python and subclassing - not necessarily like other languages
Nicely phrased

On 17/07/12 21:11, Robert Collins wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Grant Paton-Simpson  
View profile  
 More options Jul 17 2012, 8:27 am
From: Grant Paton-Simpson <gr...@p-s.co.nz>
Date: Wed, 18 Jul 2012 00:27:01 +1200
Local: Tues, Jul 17 2012 8:27 am
Subject: Re: [nzpug] Python and subclassing - not necessarily like other languages
Hi Robert,

Thanks for clarifying the distinction between subtype and subclass. Am I
right in the following: not all subclasses are subtypes because they
don't meet the criteria to be labelled as such. Calling something a
subtype means that it is a child of a particular type and that it is
interchangeable with the parent and other descendants of the parent. If
that is not true, it is not a subtype.

Having looked briefly at
http://en.wikipedia.org/wiki/Liskov_substitution_principle am I right in
thinking that the Principle only applies to subtypes and makes no
requirements of subclassing in general if you're not trying to develop a
class hierarchy (with the expectation of being able to rely on
polymorphism) but instead your goal is to allow code reuse such that
children use the resources of the parent without guaranteeing they'll
follow in their footsteps (sounds like real life ;-))?

All the best, Grant

On 17/07/12 21:11, Robert Collins wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Collins  
View profile  
 More options Jul 17 2012, 2:35 pm
From: Robert Collins <robe...@robertcollins.net>
Date: Wed, 18 Jul 2012 06:35:16 +1200
Local: Tues, Jul 17 2012 2:35 pm
Subject: Re: [nzpug] Python and subclassing - not necessarily like other languages

On Wed, Jul 18, 2012 at 12:27 AM, Grant Paton-Simpson <gr...@p-s.co.nz> wrote:
> Hi Robert,

> Thanks for clarifying the distinction between subtype and subclass. Am I
> right in the following: not all subclasses are subtypes because they don't
> meet the criteria to be labelled as such. Calling something a subtype means
> that it is a child of a particular type and that it is interchangeable with
> the parent and other descendants of the parent. If that is not true, it is
> not a subtype.

Yes, precisely. To further confuse the issue there are different
definitions that folk can use here that define substitutable
differently - but IME lkiskov substitutability is they key one,
especially in duck typed languages like Python.
Note that you can have a subtype that isn't a subclass in Python, e.g.
if you meet some defined interface, or protocol.

> Having looked briefly at
> http://en.wikipedia.org/wiki/Liskov_substitution_principle am I right in
> thinking that the Principle only applies to subtypes and makes no
> requirements of subclassing in general if you're not trying to develop a
> class hierarchy (with the expectation of being able to rely on polymorphism)
> but instead your goal is to allow code reuse such that children use the
> resources of the parent without guaranteeing they'll follow in their
> footsteps (sounds like real life ;-))?

Yes!

-Rob


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomi Richards  
View profile  
 More options Jul 17 2012, 3:53 pm
From: Thomi Richards <tho...@gmail.com>
Date: Wed, 18 Jul 2012 07:53:10 +1200
Local: Tues, Jul 17 2012 3:53 pm
Subject: Re: [nzpug] Python and subclassing - not necessarily like other languages

Hi,

This would be an excellent lightning talk to give at Kiwi PyCon 2012.

I encourage someone to condense this into a 5 minute talk!

Cheers,

--
Thomi Richards


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Grant Paton-Simpson  
View profile  
 More options Jul 17 2012, 4:31 pm
From: Grant Paton-Simpson <gr...@p-s.co.nz>
Date: Wed, 18 Jul 2012 08:31:56 +1200
Local: Tues, Jul 17 2012 4:31 pm
Subject: Re: [nzpug] Python and subclassing - not necessarily like other languages
Hi Thomi,

I'm not experienced enough in multiple OO languages but I do have a
request for whoever does step up to the plate. Please mention the
direction of the arrows between classes and subclasses like Raymond
Hettinger did at 17:25. Instead of having the arrows flowing from
classes to subclasses as is traditional (indicating that the actor is
the parent, and the child is primarily a passive recipient) show them
the other way, emphasising the way the children use resources from the
parents/grandparents. From the slide: "The pointer means 'I delegate
work to this class'".

All the best, Grant

On 18/07/12 07:53, Thomi Richards wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »