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

  • Home
  • SEARCH
  • 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 5994781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:51:59+00:00 2026-05-22T23:51:59+00:00

I have a class which contains a std::vector<Foo> where Foo is a class containing

  • 0

I have a class which contains a std::vector<Foo> where Foo is a class containing a key, value, comment, etc. Please note that there is a reason why I am using a vector and not a dictionary.

I have overloaded the subscript operator in C++ such that foos["Key Name"] will search through the vector for a Foo object with key matching “Key Name” (where foos is a std::vector<Foo>).

I use SWIG to create a Python wrapper for my library, and I would really like for this subscript operator to extend into Python. In other words, I want to be able to use the foos["Key Name"] for finding objects in the vector in Python.

Any tips on how to make SWIG recognize the subscript operator and overload it in Python? I am a little surprised that I couldn’t find examples of people doing this online. I guess most people just use a std::map and have SWIG convert it to a Python dict.

  • 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-05-22T23:52:00+00:00Added an answer on May 22, 2026 at 11:52 pm

    In straight Python, if you want to overload the subscript operator, you would create a __getitem__ and __setitem__ class method. As a simple example:

    class MyClass(object):
        def __init__(self):
            self.storage = {}
    
        def __getitem__(self, key):
            return self.storage[key]
    
        def __setitem__(self, key, value):
            self.storage[key] = value
    

    So if you want to have C++ handle this, my very best guess (no, I haven’t verified this) is that you would create a __getitem__ and __setitem__ in C++. You can either do this directly in your C++ code, or use the %extend directive within SWIG to call off to your C++ [] operator.

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

Sidebar

Related Questions

I have a class which contains a static collection that can be used across
I have a class which contains 5 properties. If any value is assingned to
I have a template class that contains a std::map that stores pointers to T
I have a class field which is a std::vector. I know how many elements
I have a vector-like class that contains an array of objects of type T
The problem: I have a class which contains a template method execute which calls
I have a MATLAB class which contains a reference to a java object classdef
I have a parent class which contains a child object. I am using set
I have a base class Node which contains a list of child nodes. Node
I have a class library project which contains some content files configured with the

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.