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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:45:09+00:00 2026-05-14T08:45:09+00:00

Should I give my class members default values like this: class Foo: num =

  • 0

Should I give my class members default values like this:

class Foo:
    num = 1

or like this?

class Foo:
    def __init__(self):
        self.num = 1

In this question I discovered that in both cases,

bar = Foo()
bar.num += 1

is a well-defined operation.

I understand that the first method will give me a class variable while the second one will not. However, if I do not require a class variable, but only need to set a default value for my instance variables, are both methods equally good? Or one of them more ‘pythonic’ than the other?

One thing I’ve noticed is that in the Django tutorial, they use the second method to declare Models. Personally I think the second method is more elegant, but I’d like to know what the ‘standard’ way is.

  • 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-14T08:45:09+00:00Added an answer on May 14, 2026 at 8:45 am

    Extending bp’s answer, I wanted to show you what he meant by immutable types.

    First, this is okay:

    >>> class TestB():
    ...     def __init__(self, attr=1):
    ...         self.attr = attr
    ...     
    >>> a = TestB()
    >>> b = TestB()
    >>> a.attr = 2
    >>> a.attr
    2
    >>> b.attr
    1
    

    However, this only works for immutable (unchangable) types. If the default value was mutable (meaning it can be replaced), this would happen instead:

    >>> class Test():
    ...     def __init__(self, attr=[]):
    ...         self.attr = attr
    ...     
    >>> a = Test()
    >>> b = Test()
    >>> a.attr.append(1)
    >>> a.attr
    [1]
    >>> b.attr
    [1]
    >>> 
    

    Note that both a and b have a shared attribute. This is often unwanted.

    This is the Pythonic way of defining default values for instance variables, when the type is mutable:

    >>> class TestC():
    ...     def __init__(self, attr=None):
    ...         if attr is None:
    ...             attr = []
    ...         self.attr = attr
    ...     
    >>> a = TestC()
    >>> b = TestC()
    >>> a.attr.append(1)
    >>> a.attr
    [1]
    >>> b.attr
    []
    

    The reason my first snippet of code works is because, with immutable types, Python creates a new instance of it whenever you want one. If you needed to add 1 to 1, Python makes a new 2 for you, because the old 1 cannot be changed. The reason is mostly for hashing, I believe.

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

Sidebar

Ask A Question

Stats

  • Questions 418k
  • Answers 418k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer char text3[100]; sprintf( text3, "%s%s%s%s", text1, text2, "2", "12345" ); May 15, 2026 at 9:53 am
  • Editorial Team
    Editorial Team added an answer I think it saves all session data in one object.… May 15, 2026 at 9:53 am
  • Editorial Team
    Editorial Team added an answer Simple things you need to do, Get face book SDK… May 15, 2026 at 9:53 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.