New __init__ style

93 views
Skip to first unread message

Cédric Krier

unread,
Aug 24, 2016, 7:00:03 AM8/24/16
to Tryton
Hi,

Since some time, I'm a little bit annoyed by our exception about
"import *" in __init__.py. It will be great if we could have no more
those warning.
So I propose to gradually change our style to use this one (example from
party module):

import category
import party


def register():
Pool.register(
category.Category,
party.Party,
party.PartyCategory,
…)

I find that it has three advantages:

- remove the "import *"
- remove collision risk about class name
- show which python file comes a class

What do you think?

--
Cédric Krier - B2CK SPRL
Email/Jabber: cedric...@b2ck.com
Tel: +32 472 54 46 59
Website: http://www.b2ck.com/

Sergi Almacellas Abellana

unread,
Aug 24, 2016, 7:15:46 AM8/24/16
to tryto...@googlegroups.com
El 24/08/16 a les 12:59, Cédric Krier ha escrit:
> Hi,
>
> Since some time, I'm a little bit annoyed by our exception about
> "import *" in __init__.py. It will be great if we could have no more
> those warning.
> So I propose to gradually change our style to use this one (example from
> party module):
>
> import category
> import party
> …
>
> def register():
> Pool.register(
> category.Category,
> party.Party,
> party.PartyCategory,
> …)
>
> I find that it has three advantages:
>
> - remove the "import *"
> - remove collision risk about class name
> - show which python file comes a class
>
> What do you think?

It thing is a very good improvement, especially third point as knowing
the python file from which the class becomes very usefull for modules
with a lot of python files.

I have an additional doubt: If we remove the "import *" we can also
remove the __all__ attribute from imported files?



--
Sergi Almacellas Abellana
www.koolpi.com
Twitter: @pokoli_srk

Jean Cavallo

unread,
Aug 24, 2016, 8:00:18 AM8/24/16
to tryto...@googlegroups.com

2016-08-24 13:15 GMT+02:00 Sergi Almacellas Abellana <se...@koolpi.com>:
I find that it has three advantages:

    - remove the "import *"
    - remove collision risk about class name
    - show which python file comes a class

What do you think?

It thing is a very good improvement, especially third point as knowing the python file from which the class becomes very usefull for modules with a lot of python files.

+1

Jean Cavallo
Coopengo

Cédric Krier

unread,
Aug 24, 2016, 8:15:03 AM8/24/16
to tryto...@googlegroups.com
On 2016-08-24 13:15, Sergi Almacellas Abellana wrote:
> I have an additional doubt: If we remove the "import *" we can also remove
> the __all__ attribute from imported files?

I think it is good practice to define what is public.

Mathias Behrle

unread,
Aug 24, 2016, 10:45:11 AM8/24/16
to tryto...@googlegroups.com
* Cédric Krier: " [tryton-dev] New __init__ style" (Wed, 24 Aug 2016 12:59:25
+0200):

> Hi,
>
> Since some time, I'm a little bit annoyed by our exception about
> "import *" in __init__.py. It will be great if we could have no more
> those warning.
> So I propose to gradually change our style to use this one (example from
> party module):
>
> import category
> import party
> …
>
> def register():
> Pool.register(
> category.Category,
> party.Party,
> party.PartyCategory,
> …)
>
> I find that it has three advantages:
>
> - remove the "import *"
> - remove collision risk about class name
> - show which python file comes a class
>
> What do you think?


Alternative:

from category import Category
from party import Party, PartyCategory
...

def register():
...no need to change


While this should meet all three advantages cited above as well, it has for me
the additional advantage of a more accustomed view of imports.


--

Mathias Behrle
MBSolutions
Gilgenmatten 10 A
D-79114 Freiburg

Tel: +49(761)471023
Fax: +49(761)4770816
http://www.m9s.biz
UStIdNr: DE 142009020
PGP/GnuPG key availabable from any keyserver, ID: 0xD6D09BE48405BBF6
AC29 7E5C 46B9 D0B6 1C71 7681 D6D0 9BE4 8405 BBF6

Cédric Krier

unread,
Aug 24, 2016, 11:00:02 AM8/24/16
to tryto...@googlegroups.com
On 2016-08-24 16:44, Mathias Behrle wrote:
> Alternative:
>
> from category import Category
> from party import Party, PartyCategory
> ...
>
> def register():
> ...no need to change
>
>
> While this should meet all three advantages cited above as well, it has for me
> the additional advantage of a more accustomed view of imports.

The disadvantages are when a module has a lot of object the list in the
import statement becomes very long, difficult to read and more
complicate to maintain (because it must be changed for every new object).
signature.asc
Reply all
Reply to author
Forward
0 new messages