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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:14:41+00:00 2026-06-11T09:14:41+00:00

I am just learning Python and I come from a C background so please

  • 0

I am just learning Python and I come from a C background so please let me know if I have any confusion / mix up between both.

Assume I have the following class:

class Node(object):
    def __init__(self, element):
        self.element = element
        self.left = self.right = None

    @classmethod
    def tree(cls, element, left, right):
        node = cls(element)
        node.left = left
        node.right = right
        return node

This is a class named Node, that overloads the constructor, to be able to handle different arguments if needed.

What is the difference between defining self.element in __init__ only (as shown above) as opposed to doing the following:

class Node(object):
    element, left, right = None
    def __init__(self, element):
        self.element = element
        self.left = self.right = None

Isn’t self.element in __init__ the same as the class’s element variable defined? Wouldn’t that just overwrite element from None to the element value passed into __init__?

  • 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-11T09:14:42+00:00Added an answer on June 11, 2026 at 9:14 am

    One is a class attribute, while the other is an instance attribute. They are different, but they are closely related to one another in ways that make them look the same at times.

    It has to do with the way python looks up attributes. There’s a hierarchy. In simple cases it might look like this:

    instance -> Subclass -> Superclass -> object (built-in type)
    

    When you look for an attribute on instance like this…

    `instance.val`
    

    …what actually happens is that first, Python looks for val in the instance itself. Then, if it doesn’t find val, it looks in its class, Subclass. Then, if it doesn’t find val there, it looks in the parent of Subclass, Superclass. This means that when you do this…

    >>> class Foo():
        foovar = 10  
        def __init__(self, val):
            self.selfvar = val
    

    …all instances of Foo share foovar, but have their own distinct selfvars. Here’s a simple, concrete example of how that works:

    >>> f = Foo(5)
    >>> f.foovar
    10
    >>> Foo.foovar
    10
    

    If we don’t touch foovar, it’s the same for both f and Foo. But if we change f.foovar…

    >>> f.foovar = 5
    >>> f.foovar
    5
    >>> Foo.foovar
    10
    

    …we add an instance attribute that effectively masks the value of Foo.foovar. Now if we change Foo.foovar directly, it doesn’t affect our foo instance:

    >>> Foo.foovar = 7
    >>> f.foovar
    5
    

    But it does affect a new foo instance:

    >>> Foo(5).foovar
    7
    

    Also keep in mind that mutable objects add another layer of indirection (as mgilson reminded me). Here, f.foovar refers to the same object as Foo.foovar, so when you alter the object, the changes are propagated up the hierarchy:

    >>> Foo.foovar = [1]
    >>> f = Foo(5)
    >>> f.foovar[0] = 99
    >>> Foo.foovar
    [99]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just started learning python and am getting caught up. I come from
I have just started learning Python & have come across namespaces concept in Python.
I have just started learning python version 3 and trying to create a file
I'm just learning about python. I'm fairly new. I have the following code that
I just started using/learning Python and have some questions. I have a text file
So I'm just learning python (I know plenty of other languages) and I'm confused
I am just learning python and Qt these days. So please consider that this
I'm just learning python (with a background in VBA). Why isn't this dictionary loading?
just considering starting to learning python but I have one concern before I invest
I just started learning Python (as my first language, so I know next to

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.