Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8534747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:16:03+00:00 2026-06-11T10:16:03+00:00

Hello fellow StackOverflowers, I’m implementing a Binary Search Tree with pretty much the same

  • 0

Hello fellow StackOverflowers,

I’m implementing a Binary Search Tree with pretty much the same interface as a dict in Python (before anyone asks, I’m doing it for fun, no production code).

For adding, retrieving and deleting elements from my tree, I’ve implemented __getitem__, __setitem__ and __delitem__, which works great.

The question is, since this is a recursive data structure, my __getitem__ method itself calls __getitem__ on either the left or right branch of the tree, if the current node does not have the key I’m looking for.

What is the most “pythonic” way of doing this recursive call, via __getitem__ or []?

Example:

def __getitem__(self, key):
    if key  == self.key:
        return self.value
    if key < self.key and self.left is not None:
        return self.left[key]
    if key > self.key and self.right is not None:
        return self.right[key]
    return None

versus

def __getitem__(self, key):
    if key  == self.key:
        return self.value
    if key < self.key and self.left is not None:
        return self.left.__getitem__(key)
    if key > self.key and self.right is not None:
        return self.right.__getitem__(key)
    return None

I know they both work exactly the same, one being a wrapper for the other, but this is a question of style.

Using [] directly provides more concise code, less verbosity, but might mislead people who don’t immediately understand that the instruction is basically the recursive call of the method, so __getitem__ removes ambiguity.

Bear in mind, I’m not talking about using one or the other in external calls, clearly [] shall be used in that case, but only inside the method, as the recursive call.

What are your thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:16:04+00:00Added an answer on June 11, 2026 at 10:16 am

    I generally use [], however it really doesn’t matter … I’m not aware of any style guide on this issue.


    Note that when you’re calling __getitem__ on a parent class, you need to use __getitem__ instead of [...]. e.g.

    class getDict(dict):
       def __getitem__(self,key):
           if key in self:
              return dict.__getitem__(self,key)
           else:
              return None
    

    But that’s not what you’re dealing with here …

    please don’t use this code — it is not meant to be an example of good code (return dict.get(self,key,None) would be better). It is only an easy to read illustration

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello fellow StackOverflowers. I'm have a brain fart right now, and I cannot seem
Hello fellow programmers! Recently, I started learning Git, and pretty quickly discovered the amazing
Hello fellow stackoverflowers, Most Ant build files I've seen online involve a clean target
Hello Fellow stackoverflowers, I´m stuck writing a piece of code. I have application with
Hello fellow StackOverflowers! I have some information in a database that I need pulled
Hello fellow StackOverflow users (or Stackoverflowers ?): I'm learning-by-coding WPF. I read several articles/saw
hello fellow android programmers, I'm constantly running into the same problem over and over
Hello fellow stackers. I am implementing node.js and nowjs on a website I am
hello fellow java developers. I'm having a bit of an issue here. I have
Hello fellow software developers. I want to distribute a C program which is scriptable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.