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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:30:26+00:00 2026-05-12T05:30:26+00:00

Being relatively new to Python 2, I’m uncertain how best to organise my class

  • 0

Being relatively new to Python 2, I’m uncertain how best to organise my class files in the most ‘pythonic’ way. I wouldn’t be asking this but for the fact that Python seems to have quite a few ways of doing things that are very different to what I have come to expect from the languages I am used to.

Initially, I was just treating classes how I’d usually treat them in C# or PHP, which of course made me trip up all over the place when I eventually discovered the mutable values gotcha:

class Pants(object):
    pockets = 2
    pocketcontents = []

class CargoPants(Pants):
    pockets = 200

p1 = Pants()
p1.pocketcontents.append("Magical ten dollar bill")
p2 = CargoPants()

print p2.pocketcontents

Yikes! Didn’t expect that!

I’ve spent a lot of time searching the web and through some source for other projects for hints on how best to arrange my classes, and one of the things I noticed was that people seem to declare a lot of their instance variables – mutable or otherwise – in the constructor, and also pile the default constructor arguments on quite thickly.

After developing like this for a while, I’m still left scratching my head a bit about the unfamiliarity of it. Considering the lengths to which the python language goes to to make things seem more intuitive and obvious, it seems outright odd to me in the few cases where I’ve got quite a lot of attributes or a lot of default constructor arguments, especially when I’m subclassing:

class ClassWithLotsOfAttributes(object):
    def __init__(self, jeebus, coolness='lots', python='isgoodfun', 
             pythonic='nebulous', duck='goose', pants=None, 
             magictenbucks=4, datawad=None, dataload=None,
             datacatastrophe=None):

        if pants is None: pants = []
        if datawad is None: datawad = []
        if dataload is None: dataload = []
        if datacatastrophe is None: datacatastrophe = []
        self.coolness = coolness
        self.python = python
        self.pythonic = pythonic
        self.duck = duck
        self.pants = pants
        self.magictenbucks = magictenbucks
        self.datawad = datawad
        self.dataload = dataload
        self.datacatastrophe = datacatastrophe
        self.bigness = None
        self.awesomeitude = None
        self.genius = None
        self.fatness = None
        self.topwise = None
        self.brillant = False
        self.strangenessfactor = 3
        self.noisiness = 12
        self.whatever = None
        self.yougettheidea = True

class Dog(ClassWithLotsOfAttributes):
    def __init__(self, coolness='lots', python='isgoodfun', pythonic='nebulous', duck='goose', pants=None, magictenbucks=4, datawad=None, dataload=None, datacatastrophe=None):
        super(ClassWithLotsOfAttributes, self).__init__(coolness, python, pythonic, duck, pants, magictenbucks, datawad, dataload, datacatastrophe)
        self.noisiness = 1000000

    def quack(self):
        print "woof"

Mild silliness aside (I can’t really help myself when cooking up these artificial example classes), assuming I have a real-world need for a set of classes with this many attributes, I suppose my questions are:

  • What is the most, uhh, ‘pythonic’ way of declaring a class with that many attributes? Is it best to put them against the class if the default is immutable, ala Pants.pockets, or is it better to put them in the constructor, ala ClassWithLotsOfAttributes.noisiness?

  • Is there a way to eliminate the need to redeclare the defaults for all of the subclass constructor arguments, as in Dog.__init__? Should I even be including this many arguments with defaults anyway?

  • 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-12T05:30:27+00:00Added an answer on May 12, 2026 at 5:30 am
    • If attributes will vary from instance
      to instance make them instance
      attribute i.e. create them
      inside__init__ using self else if they need to
      be shared between class instances
      like a constant, put them at class
      level.

    • If your class really need to pass, so
      many arguments in __init__, let
      derive class use argument list and
      keyword arguments e.g.

    class Dog(ClassWithLotsOfAttributes):
        def __init__(self, *args , **kwargs):
            super(ClassWithLotsOfAttributes,    self).__init__(*args , **kwargs)
            self.coolness = "really cool!!!
    • No need of passing all variables except few important ones, in
      __init__, class can assume some
      defaults and user can change them
      later on if needed.
    • Use 4 spaces instead of tab.

    • if you need to add an extra arg bite, to Dog and keyword arg old too

    class CoolDog(ClassWithLotsOfAttributes):
        def __init__(self, bite, *args , **kwargs):
            self.old = kwargs.pop('old', False) # this way we can access base class args too
            super(ClassWithLotsOfAttributes,    self).__init__(*args , **kwargs)
            self.bite = bite
            self.coolness = "really really cool!!!

    various ways you useCoolDog

    CoolDog(True)
    CoolDog(True, old=False)
    CoolDog(bite=True, old=True)
    CoolDog(old=True, bite=False)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Being relatively new to the .net game, I was wondering, has anyone had any
Being relatively new to functional programming, I expend lots of energy wondering is this
Being new to test based development, this question has been bugging me. How much
Being really new to wx, I'm wondering if there is an IDE (especially for
Being stuck with a legacy database schema that no longer reflects your data model
Being vaguely familiar with the Java world I was googling for a static analysis
Being a C# developer since version 1.0, F# has captured my free time for
Being primarily a C++ developer the absence of RAII (Resource Acquisition Is Initialization) in
Being a application developer, do I need to know Unicode?
Being a Windows developer I'm currently working on my own project using LAMP. I

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.