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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:10:27+00:00 2026-06-10T19:10:27+00:00

Should I write class MyClass: def __init__(self): self.field = 0 or class MyClass: def

  • 0

Should I write

class MyClass:
     def __init__(self):
         self.field = 0

or

class MyClass:
     def __init__(self):
         field = 0

Is there any difference between these statements?

  • 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-10T19:10:29+00:00Added an answer on June 10, 2026 at 7:10 pm

    Yes, you must use the self variable to set the properties of Python objects/instances.

    In the second case, you’re just creating a local variable in __init__, not defining a property.

    class MyClass:
        def __init__(self):
            field = 0
    
    print MyClass().field # an error! it's not defined!
    

    You may be misled by the way that you can just use raw assignments in class blocks to set class properties:

    class MyClass:
        a = 2
    
    print MyClass.a # 2
    

    The behaviour in class blocks is unusual, and you don’t get similar behaviour inside methods.

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

Sidebar

Related Questions

Should there be any specific order in which I should write the following for
I have the following class in python class myTest: def __init__(self, str): self.str =
Suppose, I write class A { }; The compiler should provide (as and when
I'm considering the approaches to write class MyClass { @Get @Set protected int aValue;
class MyClass(functions: Seq[Function1[Array[_], Unit]) { def foo(parametersForEachFunction: Seq[Array[_]]) { assert(functions.size == parametersForEachFunction.size) for ((function,
If I have a class like this, how should I write the copy constructor?
Here is my class that I need to pickle: class sdict(dict): def __getattr__(self, attr):
I wrote a PHP CUrl Class, if i execute Methods which should return the
I know I should write this in Vb.net but for my own reasons I'm
I have some child processes which should write some logs into a common file.

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.