<div dir="ltr"><div><div><div><div><div><div><div>I'm gonna take a shot at elaborating this point.<br><br></div>We Python programmers often tout Python as a high-level, high-productivity language where complex and useful things are easy to do. However, vanilla Python classes are anything but high level; they're basically glorified dictionaries that require an awful lot of boilerplate for basic features. __init__ verbosity is just one of the symptoms of this.<br><br></div>I'm going to posit we need declarative classes. (This is what a library like attrs provides, basically.) For a class to be declarative, it needs to be possible to inspect the class for its attributes and more.<br><br></div>I'm putting forward three examples. These examples are based on attrs since that's what I consider to be the best way of having declarative classes in Python today.<br><br></div>First example. Here's a slide from a recent talk I've given to a local Python meetup: <a href="http://tinche.github.io/talks/attrs/#/8/5">http://tinche.github.io/talks/attrs/#/8/5</a> This is a class decorator that, when applied to any attrs class, will generate a simple shallow copy() method for that class. This copy method will be faster than copy.copy() and IMO less surprising since it will actually invoke __init__ (and all logic inside). This isn't production code, just a proof of concept for the talk.<br><br></div>Second example: I'm working on a library that can turn dictionaries into attrs classes (i.e. turn unstructured data into structured data). Example: <a href="http://cattrs.readthedocs.io/en/latest/structuring.html#attrs-classes">http://cattrs.readthedocs.io/en/latest/structuring.html#attrs-classes</a>. When dealing with nested classes the library not only needs to know exactly which fields a class has but also the types of those fields, recursively (for which I'm planning to use attrs metadata). This functionality is useful when preparing data to be stored or sent elsewhere, since serialization libraries and clients in other languages expect json/msgpack etc.<br><br></div>Third example: I work at a mobile games company. The backends are Python, the games are written in other languages. The backends and the games share data structures - requests, responses, the save game, game data. I want to have a single source of truth for these structures, so I want to generate classes/structs in other languages from the backend Python classes.<br><br></div>All of these examples require declarative classes to do properly. Other approaches are, I suppose, possible in some cases - like inspecting __init__ and whatnot. But I claim the underlying issue is that we should be declaring classes differently, both to enable these new, high-level use cases and because declarative classes are much more readable and contain less boilerplate than what we have now, and this is (for me) Python is about.<br><div><div><div><div><div><div><div><div><div><div><div class="gmail_quote"><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Message: 1<br>
Date: Fri, 28 Apr 2017 09:28:55 +1000<br>
From: Steven D'Aprano <<a href="mailto:steve@pearwood.info" target="_blank">steve@pearwood.info</a>><br>
To: <a href="mailto:python-ideas@python.org" target="_blank">python-ideas@python.org</a><br>
Subject: Re: [Python-ideas] Augmented assignment syntax for objects.<br>
Message-ID: <<a href="mailto:20170427232853.GC22525@ando.pearwood.info" target="_blank">20170427232853.GC22525@ando.pearwood.info</a>><br>
Content-Type: text/plain; charset=iso-8859-1<br>
<br>
On Wed, Apr 26, 2017 at 08:52:46PM -0400, Juancarlo A?ez wrote:<br>
<br>
> In my experience, what Python is lacking is a way to declare attributes<br>
> outside of the constructor. Take a look at how it's done in C#, Swisft, or<br>
> Go.<br>
<br>
Since you apparently already know how they do it, how about telling us<br>
(1) how they do it, and (2) *why* they do it?<br>
<br>
<br>
> Object attributes outside of the constructor would solve things more<br>
> relevant than the vertical space used when assigning constructor parameters<br>
> to attributes.<br>
<br>
Solve which things?<br>
<br>
<br>
> For example, multiple inheritance is well designed in<br>
> Python, except that it often requires constructors with no parameters,<br>
> which leads to objects with no default attributes.<br>
<br>
Can you elaborate?<br>
<br>
<br>
<br>
--<br>
Steve<br>
<br>
</blockquote></div></div></div></div></div></div></div></div></div></div></div></div>