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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:45:42+00:00 2026-06-08T07:45:42+00:00

In the following code: class A(object): VALUE = 1 def __init__(self, value=VALUE): self.value =

  • 0

In the following code:

class A(object):
  VALUE = 1
  def __init__(self, value=VALUE):
    self.value = value

class B(A):
  VALUE = 2

i’d expect that B().value should be equal to 2, however:

B().value = 1

Is there an elegant way to define a class hierarchy where child classes can just declare class variables they want to override and have them be defaults for the instance variables? I still want to allow for these to be changed on a per-instance level, eg.

b = B(value=3)
  • 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-08T07:45:43+00:00Added an answer on June 8, 2026 at 7:45 am

    This is another default arguments question. The point is that when you write

    def foo(value=VALUE):
    

    the code inside the function is compiled and made into a function object. It is at this time — not at call time! — that the default arguments are stored. So by the time you have defined B it is too late: the default value of foo is already set and changing VALUE won’t have any effect.

    If this seems a strange thing to do, suppose foo was a global function:

    default = 3
    def foo(x=default): pass
    

    Then any other code, anywhere, could screw up foo by doing

    global default
    default = 4
    

    This is arguably just as confusing.


    To force the lookups to be done at runtime not compile time, you need to put them inside the function:

    def foo(value=None):
        self.value = self.VALUE if value is None else value
    

    or (not quite the same but prettier)

    self.value = value or self.VALUE
    

    (This is different because it will treat any ‘falsy’ value as a sentinel — that is, 0, [], {} etc will all be overwritten by VALUE.)


    EDIT: @mgilson pointed out another way of doing this:

    def foo(**kwargs):
        self.value = kwargs.get("value", self.VALUE)
    

    This is neater in that it doesn’t require you to make a sentinel value (like None or object(), but it does change the argument specification of foo quite fundamentally since now it will accept arbitrary keyword arguments. Your call.

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

Sidebar

Related Questions

Let's say I have the following code: class MyClass(object): def __init__(self, param): self.param =
I have some questions regarding the following code: 1 class Test(object): 2 def __init__(self):
I have code like the following: class SomeSharedData(object): def __init__(self): self._lock = RLock() self._errors
consider the following code: class MyClass(object): def __init__(self): self.data_a = np.array(range(100)) self.data_b = np.array(range(100,200))
The following code: class Log: BAT_STATS = ['AB', 'R', 'H', 'HR'] def __init__(self, type):
The following code: class MyClass(): def test(self): self.__x = 0 def __setattr__(self, name, value):
I have code like the following, class _Process(multiprocessing.Process): STOP = multiprocessing.Manager().Event() def __init__(self, queue,
Consider the following class: class SquareErrorDistance(object): def __init__(self, dataSample): variance = var(list(dataSample)) if variance
To illustrate the question check the following code: class MyDescriptor(object): def __get__(self, obj, type=None):
I have the following code that creates a serverside object of the xmlhttp class.

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.