[Python-ideas] Forward-References & Out-of-order declaration

Sven R. Kunze srkunze at mail.de
Wed Aug 26 00:11:09 CEST 2015


On 25.08.2015 22:48, Guido van Rossum wrote:
> On Tue, Aug 25, 2015 at 11:03 AM, Sven R. Kunze <srkunze at mail.de 
> <mailto:srkunze at mail.de>> wrote:
>
>     While reading material for implementing some kind of import hooks
>     and so forth for the xfork package, I came across this one:
>     http://stackoverflow.com/questions/16907186/python-model-inheritance-and-order-of-model-declaration
>
>     Reactivated in my mind, after reading Prof. Humbert's request of
>     adding a "more forwarded" referencing of classes, I just wanted to
>     ask:
>
>     1) What is the general opinion regarding having a more declarative
>     style when writing modules?
>
>
> It would be a nice idea for a different language. Python's execution 
> model would have to be changed dramatically in order to support this 
> (outside the very narrow case of annotations or the cases where it 
> already works). This just isn't going to happen -- too much code 
> relies on the existing execution model.

I can totally understand that and I am not a friend of hard cut/dramatic 
changes and so forth myself. It's just that I was pondering multiple 
times over this and I am usual sort of: "there must be a way and it must 
work smoothly".

What I asked about is basically a reduction of functionality. Thus, the 
result of that reduction is a subset of Python and so still valid 
Python. So, I can think of two ways in order to test it out:
- another keyword instead of 'import' (e.g. 'use') that does not 
actually execute the module but construct its dict from its AST
or
- mark the modules alike the pyxl magic encoding you referred to lately 
and thus enforce this sort of declaration model

Don't get me wrong. Most of the time, I love Python's spontaneous nature 
of getting things done on the module level; and I never want that to go 
away. However, once your code base matures, you need to shape your code 
the right way and massage it in some separate packages and modules. Most 
of these libs are just of a declarative style (e.g. 5 classes and 3 
functions, period); nothing more is necessary here.

Also the issue of failing cyclic imports can be solved by some sort of 
"restricted/declarative importing". Not a huge issue by itself and most 
of the time, one can fix it by scratching one's head enough; it's 
time-consuming nevertheless.

>     2) That given, what is the general opinion about introducing the
>     out-of-order declaration asked for in the StackOverflow posts?
>
>
> I couldn't extract an actual proposal from the stackoverflow link you 
> gave.

Not sure if that makes sense anymore, but I think it's worth at least to 
get the idea across:


The issue (not a huge problem again but annoying from time to time) is 
that the order of declaration in a module actually matters. IIRC other 
modern languages like C# don't require you do actually care about this 
anymore.

Possible example (for whatever reason an author wants to do that -- also 
cf. stackoverflow):

class UseThis(Base):
     pass

class UseThat(Base):
     pass

class Base:
     pass

In that regard, Python feels a bit rusty.

Best,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150826/53f3dc6d/attachment-0001.html>


More information about the Python-ideas mailing list