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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:07:01+00:00 2026-05-16T17:07:01+00:00

I am writing a python function with the following: class myObj(object): def __init__(self, args):

  • 0

I am writing a python function with the following:

class myObj(object):
    def __init__(self, args):
        # there is code here
    def newO(self, name, description):
        if type(name)==str:
            self.oname.append(name)
            self.o.append(description)
        elif type(name)==list:
             for n in name:
                self.oname.append(n)
                self.o.append(description)

However I am led to believe this is not the best way to accomplish this. Whats a better way?
I want to be able to call newO with a string or a list.

  • 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-16T17:07:02+00:00Added an answer on May 16, 2026 at 5:07 pm
    1. Never check type(x) == foo, use isinstance(x, foo) — the former will break if there is a subclass, for instance.

    2. You appear to be maintaining parallel lists. If the order matters, it might make more sense to use a list of tuples instead, so self.values.append((name, description)). If the order does not matter, a dictionary would be better: self.values[name] = description (with the assumption that you don’t want duplicate names).

    3. Single letter variables are a no-no.

    4. If you want to call newO with a string or a list, what would the disadvantage be of splitting it into two functions? Perhaps (and I might be wrong) you come from a language with compile-time polymorphism, where a string and a list would be dispatched differently. In reality they are treated as different functions, so in a dynamic language you might split them up like this:

      def add(self, name, description):
          ...
      def add_many(self, name_list, description):
          for name in name_list:
              self.add(name, description)
      

      It’s possible that you don’t know a priori whether you have a string or a list, but I hope that’s not the case.

    5. Is the case of a single string really different from the case of a list of strings? Why not just use a one-element list, to remove the conditional? You could use varargs as well, to make the syntax more bulletproof:

      def add_names(self, description, *name_list):
          for name in name_list:
              self.add(name, description)
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following class: class SquareErrorDistance(object): def __init__(self, dataSample): variance = var(list(dataSample)) if variance
I'm writing a simple xmlrpc programe in python. something like the following: def foo(data):
I was writing a python function that looked something like this def foo(some_list): for
I'm writing the following function to format some text from a file object 'item':
Kinda knew to Python: I have the following code: def printCSV(output, values, header): 63
I'm writing a little Python extension in C/C++, and I've got a function like
I have been writing Python code for only a couple of weeks, so I'm
I am writing Python code in Visual Studio 2010 using the excellent Python Tools
I am writing a native function that will return multiple Python objects PyObject *V
I am writing a negative lookbehind assertion expression in Python which performs the following

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.