PyWart: Module access syntax

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Jan 11 11:02:34 EST 2013


On Thu, 10 Jan 2013 22:01:37 -0800, Rick Johnson wrote:

> Python's module/package access uses dot notation.
> 
>   mod1.mod2.mod3.modN
> 
> Like many warts of the language, this wart is not so apparent when first
> learning the language. The dot seems innocently sufficient, however, in
> truth it is woefully inadequate! Observe:
> 
>  name1.name2.name3.name4.name5
> 
> Can you tell me which names are modules, which are classes, and which
> are methods/functions? Wait, i know the argument you'll use:
> 
>   """I can decipher from spelling! Modules use all lowercase, classes
>   use initial uppercase, and methods use words_sep_by_underscore. I'm so
>   smart!"""

Wrong. My answer is... 

Who cares? Why do you care which names are modules, classes, etc? This is 
Python, and duck-typing rules!

Today I have:

x = mylib.stuff.spam().parrot.speak()

where:

- mylib is a module
- stuff is a separate module imported into mylib
- spam is a class
- parrot a data attribute
- and speak a method. 

But then tomorrow I re-factor the code and turn:

- mylib into a package
- stuff into a module of the package
- spam into a factory function
- parrot into a property
- and speak into a dynamically generated static method created using 
__getattr__.

How do I write this new code? Exactly the same as the old code -- no 
changes are required!

x = mylib.stuff.spam().parrot.speak()

*It doesn't matter* what kind of entity each of the dotted names 
represent, so long as they have the expected interface.


> The solution is obvious by using proper syntax.

Solution to what? You can only have a solution once you have identified a 
problem. You have not identified a problem. In any case, your suggestion 
is *not* obvious.

>  import lib:gui:tkinter:dialogs.SimpleDialog as Blah

Which names are packages, modules, classes, methods, functions, or other 
objects?

Why do you have lib:gui but dialogs.SimpleDialog? Is the rule "classes 
should always be preceded by a dot?"

Following this import:

import lib:gui

how do I access attributes of that module? 

x = lib:gui:function() perhaps? Now I have to remember which attribute 
access uses dot operator and which uses colon operator.

Does this mean there needs to four new be special methods:

__getcolonattribute__
__getcolonattr__
__setcolonattr__ 
__delcolonattr__ 

to match the regular dot operator methods

__getattribute__
__getattr__ 
__setattr__ 
__delattr__ 

? Or do you think that the Python compiler should special-case attribute 
access from modules?


> You /could/ use two colons:
>  
>  import lib::gui::tkinter::dialogs.SimpleDialog as Blah

Before thinking about the syntax, you need to think about the behaviour, 
identify what actual problem you hope to solve, and how you hope to solve 
it. Not just throw random syntax around and hope that it's "obvious".



-- 
Steven



More information about the Python-list mailing list