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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:09:29+00:00 2026-05-28T18:09:29+00:00

I noticed that in Python, people initialize their class attributes in two different ways.

  • 0

I noticed that in Python, people initialize their class attributes in two different ways.

The first way is like this:

class MyClass:
  __element1 = 123
  __element2 = "this is Africa"

  def __init__(self):
    #pass or something else

The other style looks like:

class MyClass:
  def __init__(self):
    self.__element1 = 123
    self.__element2 = "this is Africa"

Which is the correct way to initialize class attributes?

  • 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-28T18:09:29+00:00Added an answer on May 28, 2026 at 6:09 pm

    Neither way is necessarily correct or incorrect, they are just two different kinds of class elements:

    • Elements outside the __init__ method are static elements; they belong to the class.
    • Elements inside the __init__ method are elements of the object (self); they don’t belong to the class.

    You’ll see it more clearly with some code:

    class MyClass:
        static_elem = 123
    
        def __init__(self):
            self.object_elem = 456
    
    c1 = MyClass()
    c2 = MyClass()
    
    # Initial values of both elements
    >>> print c1.static_elem, c1.object_elem 
    123 456
    >>> print c2.static_elem, c2.object_elem
    123 456
    
    # Nothing new so far ...
    
    # Let's try changing the static element
    MyClass.static_elem = 999
    
    >>> print c1.static_elem, c1.object_elem
    999 456
    >>> print c2.static_elem, c2.object_elem
    999 456
    
    # Now, let's try changing the object element
    c1.object_elem = 888
    
    >>> print c1.static_elem, c1.object_elem
    999 888
    >>> print c2.static_elem, c2.object_elem
    999 456
    

    As you can see, when we changed the class element, it changed for both objects. But, when we changed the object element, the other object remained unchanged.

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

Sidebar

Related Questions

I've noticed a discrepancy in the way that python parameters are called. In every
While answering Static class variables in Python I noticed that PythonWin PyWin32 build 209.2
I have noticed that in Python some of the fundamental types have two types
I've noticed that Java, Python, Perl, and Haskell/Clojure have distinctively different treatment of static
I noticed in the main Django introductin they show a feature that maps python
I am reading cpython code for python 3k and I have noticed, that __missing__
i noticed that paypal displays a very different favicon, one that's not just a
I noticed that the Python 2.7 documentation includes yet another command-line parsing module. In
Like many other people posting questions here, I recently started programming in Python. I'm
I've noticed that when I'm using python, I'll occasionally make a typographical error and

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.