Constructor and Method Parameters

I am writing a URL router in Python 3.1 and wondering if it is more than a taste to use one of the following options:

Tuples as constructor parameters:

router = Router( 
    (r"/item/{id}", ItemResource()),
    (r"/article/{title}", ArticleResource())
)

Method call

router = Router()
router.connect(r"/item/{id}", ItemResource())
router.connect(r"/article/{title}", ArticleResource())

Do you see any advantages or disadvantages here?

+3
source share
4 answers

I prefer passing tuples to the constructor for two reasons that Alex did not mention (at least not explicitly).

  • readability. If accessible to any code reader, that Router instance requires a route list to be accessible.

  • Lose traction. Client code should not bother initializing router instances. This is the responsibility of the Router class.

, Router Router.__init__. , RoutesList:

routes = RoutesList((r"/item/{id}", ItemResource()),
                    (r"/article/{title}", ArticleResource()))

router = Router(routes)

, , . , , , routes , ( ). : , Router : RoutesList (, , undefined ).

, ;)

+2

, , , , , , , (, disconnect connect , ?), "" , .

, " " ( , connect;-), - , , (, , ).

, , , ..., , , & . , " " .

+2

, , , .

, , .

0
0
source

Source: https://habr.com/ru/post/1764558/


All Articles