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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:35:14+00:00 2026-05-23T12:35:14+00:00

I have an object that needs to have some 4-5 values passed to it.

  • 0

I have an object that needs to have some 4-5 values passed to it. To illustrate:

class Swoosh():
    spam = ''
    eggs = ''
    swallow = ''
    coconut = ''

    [... methods ...]

Right now, the way to use Swoosh is:

swoosh = Swoosh()
swoosh.set_spam('Spam!')
swoosh.set_eggs('Eggs!')
swoosh.set_swallow('Swallow!')
swoosh.set_coconut('Migrated.')

I’m having doubts whether this is Pythonic or is this too much Java influence. Or is it just necessary? Also, in case you’re wondering why I’m using setters instead of simply accessing the object variables – some basic validation has to be done there, hence the set_ methods.

I reckon I could provide a different way to initialize a Swoosh – provide an __init__() that would accept a dict/list?

Anyway, I’m just looking for help/advice from someone more experienced with this stuff.

Thanks in advance for any input on this.

  • 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-23T12:35:14+00:00Added an answer on May 23, 2026 at 12:35 pm

    Firstly you’re using old style classes. You really, really should be using new style classes that inherit from object:

    class Swoosh(object):
    

    Defining an __init__ method that takes arguments is definitely the Pythonic way of doing things:

    def __init__(self,spam,eggs,swallow,coconut):
        self.spam = spam
        self.eggs = eggs
        self.swallow = swallow
        self.coconut = coconut
    

    This would allow you to do:

    s = Swoosh('Spam!','Eggs','Swallow','Migrated')
    

    Like any other Python function the __init__ method can have default values for arguments, e.g.

    def __init__(self,spam,eggs,swallow,coconut='Migrated.'):
            self.spam = spam
            self.eggs = eggs
            self.swallow = swallow
            self.coconut = coconut
    

    If you want to validate attributes as they’re set you should use standard property attributes rather than creating your own setters. If you do it this way you can use myobject.spam in your code like with an ordinary attribute but your setter method is still run.

    For example:

    @property
    def spam(self):
        """I'm the 'spam' property."""
        return self._spam
    
    @spam.setter
    def spam(self, value):
        if not value.endswith("!"):
            raise ValueError("spam must end with !")
        # Store the value in "private" _spam attribute
        self._spam = value
    
    @spam.deleter
    def spam(self):
        del self._spam
    

    Note: property will not work unless you’re using new-style classes as described above.

    In your example you have:

    class Swoosh():
        spam = ''
        eggs = ''
        swallow = ''
        coconut = ''
    

    This is often presented as a quick way of setting defaults for attributes for an object. It’s fine but you need to understand what is actually happening. What’s going on is that you’re setting attributes on the class. If an object doesn’t have an attribute Python will look to see if it’s defined on the class and return the value from there (as this is what you want for methods).

    If you want to set default values for object attributes you’re much better off setting default values for the arguments in __init__ as described above rather than using class attributes.

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

Sidebar

Related Questions

I am using C# 2.0 with Nunit Test. I have some object that needs
I have an object that needs a test if the object data is valid.
When you have a domain object that needs to display as an interface control,
Let's say i have a griffon object that needs to be part of the
In an embedded program I have a screen object that needs to manage a
Here is the scenario: I have an object called a Transaction that needs to
I have a method that is called on an object to perform some business
I have an object array that is being passed and then split up into
I have some calculated values in the core data database that I need to
Imagine this case where I have an object that I need to check a

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.