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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:44:25+00:00 2026-06-09T21:44:25+00:00

The way I usually declare a class variable to be used in instances in

  • 0

The way I usually declare a class variable to be used in instances in Python is the following:

class MyClass(object):
  def __init__(self):
    self.a_member = 0

my_object = MyClass()
my_object.a_member    # evaluates to 0

But the following also works. Is it bad practice? If so, why?

class MyClass(object):
  a_member = 0

my_object = MyClass()
my_object.a_member    # also evaluates to 0

The second method is used all over Zope, but I haven’t seen it anywhere else. Why is that?

Edit: as a response to sr2222’s answer. I understand that the two are essentially different. However, if the class is only ever used to instantiate objects, the two will work he same way. So is it bad to use a class variable as an instance variable? It feels like it would be but I can’t explain why.

  • 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-09T21:44:26+00:00Added an answer on June 9, 2026 at 9:44 pm

    The question is whether this is an attribute of the class itself or of a particular object. If the whole class of things has a certain attribute (possibly with minor exceptions), then by all means, assign an attribute onto the class. If some strange objects, or subclasses differ in this attribute, they can override it as necessary. Also, this is more memory-efficient than assigning an essentially constant attribute onto every object; only the class’s __dict__ has a single entry for that attribute, and the __dict__ of each object may remain empty (at least for that particular attribute).

    In short, both of your examples are quite idiomatic code, but they mean somewhat different things, both at the machine level, and at the human semantic level.


    Let me explain this:

    >>> class MyClass(object):
    ...       a_member = 'a'
    ...
    >>> o = MyClass()
    >>> p = MyClass()
    >>> o.a_member
    'a'
    >>> p.a_member
    'a'
    >>> o.a_member = 'b'
    >>> p.a_member
    'a'
    

    On line two, you’re setting a “class attribute”. This is litterally an attribute of the object named “MyClass”. It is stored as MyClass.__dict__['a_member'] = 'a'. On later lines, you’re setting the object attribute o.a_member to be. This is completely equivalent to o.__dict__['a_member'] = 'b'. You can see that this has nothing to do with the separate dictionary of p.__dict__. When accessing a_member of p, it is not found in the object dictionary, and deferred up to its class dictionary: MyClass.a_member. This is why modifying the attributes of o do not affect the attributes of p, because it doesn’t affect the attributes of MyClass.

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

Sidebar

Related Questions

Normal way: class A: def __init__(self): self.a.b.c = 10 def another_method(self): self.a.b.c = self.a.b.c
Any way to declare a new variable in F# without assigning a value to
I usually have to check things like: if ['Bob','Mary','John'].include? @user.name Is there a way
When I run an MSI (without parameters) I usually have to click my way
Why should I use #' together with lambda? It is usually written that way,
The way I understand it, we are encouraged to name Ember instances lowercase: App.mailtruck
I usually see this question asked the other way, such as Must every ivar
My interface declarations usually (always?) follow the same scheme. Here's an example: class Output
Being relatively new to Python 2, I'm uncertain how best to organise my class
there is something i still don't get. for every class i declare there is

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.