Skip to content

Don't use dunder instance attributes or methods #9

Description

@peterbe

Class instance attributes called __dunder_name are not override'able. For example, you can't do this:

class MyKeyCDNAPI(keycdn.Api):
    def __execute(self, *a, **k):  # Never gets called!
         blablabla

As a point, why are any of them private at all? What's the point of hiding things like __api_key? Why not just call it api_key. Then you don't need the setter or the getter. E.g.:

ENDPOINT = 'https://api.keycdn.com'

class Api:
     def __init__(self, api_key, endpoint=ENDPOINT):
          self.api_key = api_key
          self.endpoint = endpoint

    ... 
    def get(...):
    def post(...):
    def put(...):
    def delete(...):

    def _execute(self, call, **params):  # the only one that makes sense to make private
        ...

a = Api(MY_API_KEY)
print(a.endpoint)
print(a.api_key)
a.api_key = MY_OTHER_API_KEY

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions