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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:21:27+00:00 2026-06-11T22:21:27+00:00

I have a design issue. I would like to write a method accepts either

  • 0

I have a design issue. I would like to write a method accepts either a single object or an iterable of objects. For example, lets say I have the class Dog:

class Dog(object):
    """
    An animal with four legs that may slobber a lot
    """
    def __init__(self, name="Fido"):
       self.name = name

Now let’s say that I have a class that uses the Dog class, say DogWalker:

class DogWalker(object):
    """
    Someone who cares for Fido when Human Companion is not available
    """
    def __init__(self):
        self.dogs_walked = 0
        self.dogs_watered = 0

    def walk(self, dogs):
        """
        Take a dog for a walk
        """
        # This is not Pythonic
        if isinstance(Dog, dogs):
            # Walk a single dog
            pass
            self.dogs_walked +=1
        else:
            # walk lots of dogs
            pass


    def water(self, dogs):
        """
        Provide water to dogs
        """
        # More pythonic
        try:
            # Try to water single dog
            ...
            self.dogs_walked += 1
        except  AttributeError:
            # Try to water lots of dogs
            pass

In the example, above I’ve implemented two methods walk and water. The water method is more pythonic in that it uses Duck Typing. However, I’d like to take the conversation a bit further. Let’s say I have a care taker class that can water different types of animals:

class CareTaker(object):
    """
    Someone who cares for things
    """
    def __init__(self):
        self.things_watered = 0

    def water(self, things):
        """
        Provide water to a thing
        """
        # Here is where I need help!
        # 1. I need to figure out if things is a single item or iterable
        # 2. If thing is a single item I need to figure out what type of thing a thing is
        # 3. If thing is an iterable, I need to figure out what type of things are in the iterable.
        pass

Now, one thing that occurred to me is that each thing could know how to water itself, and then the care taker would only need to call the things water method. For example:

class CareTaker(object):
    """
    Someone who cares for things
    """
    def __init__(self):
        self.things_watered = 0

    def water(self, thing):
        """
        Provide water to a thing
        """
        result = thing.water()
        self.things_watered += 1
        return result

That way using the code might look like:

ct = CareTaker()
dog = Dog()
bird = Bird()
water_dog = ct.water(dog)
water_bird = ct.water(bird)

things = [dog, bird]

for thing in things:
    ct.water(thing)

I have a few other ideas, but I would like to get some input from others who might have faced such an issue, before I take a specific design approach. If you could list pros and cons of your ideas as well. That would be a bonus! Thanks.

UPDATE: There appear to be two equally good suggestions so far.

  1. Test the behavior of the arguments passed in, for example, write an
    is_itereable method.
  2. Use positional arguments and iterate over the list of items in the positional argument list.

I have yet to determine which is better for my actual problem.

  • 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-11T22:21:29+00:00Added an answer on June 11, 2026 at 10:21 pm

    You could use the *args “wildcard” positional parameter:

    def walk(self, *dogs):
        for dog in dogs:
            # handle each dog.
    

    and call this as:

    owner.walk(onedog, anotherdog)
    

    or

    owner.walk(*listofdogs)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a design issue, not complicated actually, but i would like to find
I have to design a GUI using Qt. I would like to draw multiple
I have this design which I cannot seem to get right, I would like
I have run into this issue several times in the past and would like
I have a design issue that I would appreciate some input on. I would
I have run into a bit of a design issue with my code. I
I'm having a design/css issue. I'm using Twitter Bootstrap and have a .row-fluid, with
I have a design issue. I am modifying existing code and where I was
I have looked on line for information that would help me solve a design
I have in mind a design problem that does not look like complex, but

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.