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 953469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:03:22+00:00 2026-05-16T00:03:22+00:00

Suppose I have the following object: class Foo(object): def __init__(self, name=None): self.name = name

  • 0

Suppose I have the following object:

class Foo(object):
  def __init__(self, name=None):
    self.name = name

  def __repr__(self):
    return self.name

And a list containing multiple instances, such as:

list = [Foo(name='alice'), Foo(name='bob'), Foo(name='charlie')]

If I want to find an object with a given name, I could use the following:

def get_by_name(name, list):
  return [foo for foo in list if foo.name == name][-1]

Which would obviously mean:

print get_by_name('alice', list)
>> alice

However, is there a more efficient data structure or method for retrieving such objects? In reality, the object names are only known at runtime and could in theory change throughout the lifecycle of the object.

Any advice?

UPDATE:

Thanks to Matt Joiners answer, I have updated it to support multiple Foo‘s with the same name:

class Foo(object):
    _all_names = {}    
    def __init__(self, name=None):
        self._name = None
        self.name = name        
    @property
    def name(self):
        return self._name        
    @name.setter
    def name(self, name):
        if self._name is not None:
            self._all_names[self._name].remove(self)
        self._name = name
        if name is not None:
            self._all_names.setdefault(name, []).append(self)
    @classmethod
    def get_by_name(cls, name):
        return cls._all_names[name]        
    def __repr__(self):
        return "{0}".format(self.name)

l = [Foo("alice"), Foo("bob"), Foo('alice'), Foo('charlie')]
print Foo.get_by_name("alice")
print Foo.get_by_name("charlie")

Any comments on this approach?

  • 1 1 Answer
  • 3 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-16T00:03:23+00:00Added an answer on May 16, 2026 at 12:03 am

    Try this for size:

    class Foo(object):
        _all_names = {}
        def __init__(self, name=None):
            self.name = name
        @property
        def name(self):
            return self._name
        @name.setter
        def name(self, name):
            self._name = name
            self._all_names[name] = self
        @classmethod
        def get_by_name(cls, name):
            return cls._all_names[name]
        def __str__(self):
            return "Foo({0})".format(self.name)
    
    a = Foo("alice")
    b = Foo("bob")
    print Foo.get_by_name("alice")
    

    Keep in mind it’s kept minimal to convey the idea, there are many tweaks and checks you can make here and there.

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

Sidebar

Related Questions

Suppose we have the following class hierarchy: class ClassA: @property def foo(self): return hello
Suppose we have following code defined in tester.py class Tester( object ): def method(
Suppose I have a the following in Scala object Foo { var functions: List[String
Suppose that I have the following python base class: class BaseClass(object): def a(): This
Suppose I have the following service object public class UserService { @Autowired private UserDao
Suppose I have the following Scala code: class Foo(a: Int) class Bar(b: Buffer[Int]) extends
Suppose I have the following class hierarchy: class A { int foo; virtual ~A()
Suppose you have the following object hierarchy: class Vehicle { public: virtual ~Vehicle() {}
Suppose I have the following: public class Foo { private ReadingList mReadingList = new
Propose to consider the following problem. Suppose we have some composite object. Are there

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.