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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:14:25+00:00 2026-06-16T17:14:25+00:00

Possible Duplicate: Variables inside and outside of a class __init__() function I understand that

  • 0

Possible Duplicate:
Variables inside and outside of a class __init__() function

I understand that when a class is called it will run the code in __init__ before anything. I still don’t see the difference between that, and writing the code directly under the class.

For example:

class main():
  x = 1

  def disp(self):
    print self.x
class main():
  def __init__(self):
    self.x = 1

  def disp(self):
    print self.x

To me, both have the same functionality. (Maybe I am missing out something.) I would like to know which is more (ahem) pythonic and 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-16T17:14:27+00:00Added an answer on June 16, 2026 at 5:14 pm

    There are a couple key differences here, both between __init__ and writing it just under the class, as well as what you wrote.

    Working with x = 1

    First, you are right–these two items of code do effectively the same thing for your purposes (specifically because we’re working with int objects here, this would be different for mutable objects):

    Note that they don’t actually do the same thing–please see the comments on this answer for clarification.

    class main(object):
        x = 1
    

    class main(object):
        def __init__(self):
            self.x = 1
    

    This is why many non-standard Python libraries, like mongoengine and django models, have a standard where you create classes without using an __init__ statement so as not to override the built-in one, but still allowing you to create class attributes, e.g., a Django example:

    class mymodel(models.model):
        name = models.CharField(max_length=20)
        url = models.UrlField()
    

    However, as the other poster points out, there is a difference between the two in that when x=1 is outside of the __init__ function, it is part of the class itself even when not intialized–see Zagorulkin Dmitry‘s answer for more detail on that. In most cases, though, that distinction won’t be relevant for you.

    Other considerations

    There are more uses for __init__ beyond just setting variables. The most important one offhand is the ability to accept arguments during initialization. To my knowledge, there is not a way to do this without an __init__ function. I’ll show you what I mean by this in this example.

    Let’s say we’re creating a Person class, and when we create a Person, we supply their age, and then their birth year is automatically calculated from that for us.

    import datetime
    class Person(object):
        def __init__(self, age):
            self.age = age
            self.birth_year = (datetime.date.today() - datetime.timedelta(days=age*365)).year
    

    In use:

    >>>joe = Person(23)
    >>>joe.age
    23
    >>>joe.birth_year
    1990
    

    This wouldn’t be possible without __init__, since we couldn’t pass the initialization the age argument otherwise.

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

Sidebar

Related Questions

Possible Duplicate: can't access global variables inside a usort function? I've experienced this problem
Possible Duplicate: Declaring variables inside or outside of a loop Please consider these 2
Possible Duplicate: JavaScript variables declare outside or inside loop? So..I've seen many articles saying
Possible Duplicate: Passing SQLite variables in Python I have a function in my script
Possible Duplicate: Overriding static variables when subclassing I have a set of classes that
Possible Duplicate: Static class variables in Python What is the Python equivalent of static
Possible Duplicate: Cannot refer to a non-final variable inside an inner class defined in
Possible Duplicate: php function variable scope I am using below code to test with
Possible Duplicate: Cannot refer to a non-final variable inside an inner class defined in
Possible Duplicate: Cannot refer to a non-final variable inside an inner class defined in

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.