Re: [Python-Dev] Confirming status of new modules in 3.4

2014-03-16 Thread Charles-François Natali
2014-03-15 21:44 GMT+00:00 Nikolaus Rath :

> Guido van Rossum  writes:
> > This downside of using subclassing as an API should be well known by now
> > and widely warned against.
>
> It wasn't known to me until now. Are these downsides described in some
> more detail somewhere?
>

The short version is: "inheritance breaks encapsulation".

As a trivial and stupid example, let's say you need a list object which
counts the number of items inserted/removed (it's completely stupid, but
that's not the point :-):

So you might do something like:
"""
class CountingList(list):

[...]

def append(self, e):
self.inserted += 1
return super().append(e)

def extend(self, l):
self.inserted += len(l)
return super().extend(l)

"""

Looks fine, it would probably work.

Now, it's actually very fragile: imagine what would happen if list.extend()
was internally implemented by calling list.append() for each element: you'd
end up counting each element twice (since the subclass append() method
would be called).

And that's the problem: by deriving from a class, you become dependent of
its implementation, even though you're using its public API. Which means
that it could work with e.g. CPython but not Pypy, or break with a new
version of Python.

Another related problem is, as Guido explained, that if you add a new
method in the subclass, and the parent class gains a method with the same
name in a new version, you're in trouble.

That's why advising inheritance as a silver bullet for code reuses is IMO
one of the biggest mistakes in OOP, simply because although attractive,
inheritance breaks encapsulation.

As a rule of thumb, you should only use inheritance within a
module/package, or in other words only if you're in control of the
implementation.

The alternative is to use "composition"

For more details, I highly encourage anyone interested in looking at the
book "Effective Java" by Joshua Bloch (the example above is inspired by his
book). Although Java-centric, it's packed with many advises, patterns and
anti-patterns that are relevant to OOP and just programming in general
(it's in my top-5 books).

cf
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Confirming status of new modules in 3.4

2014-03-16 Thread Nikolaus Rath
Charles-François Natali  writes:
> 2014-03-15 21:44 GMT+00:00 Nikolaus Rath :
>
>> Guido van Rossum  writes:
>> > This downside of using subclassing as an API should be well known by now
>> > and widely warned against.
>>
>> It wasn't known to me until now. Are these downsides described in some
>> more detail somewhere?
>>
>
> The short version is: "inheritance breaks encapsulation".
>
> As a trivial and stupid example, let's say you need a list object which
> counts the number of items inserted/removed (it's completely stupid, but
> that's not the point :-):
>
> So you might do something like:
[...]

Very illuminating example, thanks a lot!


Best,
-Nikolaus

-- 
Encrypted emails preferred.
PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6  02CF A9AD B7F8 AE4E 425C

 »Time flies like an arrow, fruit flies like a Banana.«
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


[Python-Dev] [RELEASED] Python 3.4.0

2014-03-16 Thread Larry Hastings


On behalf of the Python development team, I'm thrilled to announce
the official release of Python 3.4.


Python 3.4 includes a range of improvements of the 3.x series, including
hundreds of small improvements and bug fixes.  Major new features and
changes in the 3.4 release series include:

* PEP 428, a "pathlib" module providing object-oriented filesystem paths
* PEP 435, a standardized "enum" module
* PEP 436, a build enhancement that will help generate introspection
   information for builtins
* PEP 442, improved semantics for object finalization
* PEP 443, adding single-dispatch generic functions to the standard library
* PEP 445, a new C API for implementing custom memory allocators
* PEP 446, changing file descriptors to not be inherited by default
   in subprocesses
* PEP 450, a new "statistics" module
* PEP 451, standardizing module metadata for Python's module import system
* PEP 453, a bundled installer for the *pip* package manager
* PEP 454, a new "tracemalloc" module for tracing Python memory allocations
* PEP 456, a new hash algorithm for Python strings and binary data
* PEP 3154, a new and improved protocol for pickled objects
* PEP 3156, a new "asyncio" module, a new framework for asynchronous I/O


To download Python 3.4.0 visit:

http://www.python.org/download/releases/3.4.0/


This is a production release.  Please report any issues you notice to:

 http://bugs.python.org/


Enjoy!


--
Larry Hastings, Release Manager
larry at hastings.org
(on behalf of the entire python-dev team and 3.4's contributors)
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


[Python-Dev] default hg.python.org/cpython is now 3.5

2014-03-16 Thread Larry Hastings



The "3.4" branch is now checked in.  It contains all the 3.4 releases 
since 3.4.0rc1.  Its current state is effectively 3.4.1.


The "default" branch is now 3.5.

Cry havoc, and let slip the dogs of war,


//arry/
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] getattr vs hashattr

2014-03-16 Thread Chris Withers

On 03/12/2014 04:49 PM, Chris Angelico wrote:

You can use hasattr() in place of AttributeError


Is that true now? It used to be that hasattr swallowed all exceptions 
rather than just AttributeError making is a very dangerous weapon for 
anything (such as an orm or odb) that might do something interesting 
when you try and get an attribute from it.


On 12/03/2014 21:37, Tres Seaver wrote:

I use:

  getattr(subject, attrname, default)?

*all the time*.


On this one, did anything ever come of making getattr have a default 
default of None?


Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com