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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:11:03+00:00 2026-06-05T13:11:03+00:00

I want to clarify how variables are declared in Python. I have seen variable

  • 0

I want to clarify how variables are declared in Python.

  1. I have seen variable declaration as
class writer:
     path = ""

sometimes, there is no explicit declaration but just initialization using __init__:

def __init__(self, name):
    self.name = name

I understand the purpose of __init__, but is it advisable to declare variable in any other functions?

  1. How can I create a variable to hold a custom type?
class writer:
    path = "" # string value
    customObj = ??
  • 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-05T13:11:06+00:00Added an answer on June 5, 2026 at 1:11 pm

    Okay, first things first.

    There is no such thing as “variable declaration” or “variable initialization” in Python.

    There is simply what we call “assignment”, but should probably just call “naming”.

    Assignment means “this name on the left-hand side now refers to the result of evaluating the right-hand side, regardless of what it referred to before (if anything)”.

    foo = 'bar' # the name 'foo' is now a name for the string 'bar'
    foo = 2 * 3 # the name 'foo' stops being a name for the string 'bar',
    # and starts being a name for the integer 6, resulting from the multiplication
    

    As such, Python’s names (a better term than “variables”, arguably) don’t have associated types; the values do. You can re-apply the same name to anything regardless of its type, but the thing still has behaviour that’s dependent upon its type. The name is simply a way to refer to the value (object). This answers your second question: You don’t create variables to hold a custom type. You don’t create variables to hold any particular type. You don’t “create” variables at all. You give names to objects.

    Second point: Python follows a very simple rule when it comes to classes, that is actually much more consistent than what languages like Java, C++ and C# do: everything declared inside the class block is part of the class. So, functions (def) written here are methods, i.e. part of the class object (not stored on a per-instance basis), just like in Java, C++ and C#; but other names here are also part of the class. Again, the names are just names, and they don’t have associated types, and functions are objects too in Python. Thus:

    class Example:
        data = 42
        def method(self): pass
    

    Classes are objects too, in Python.

    So now we have created an object named Example, which represents the class of all things that are Examples. This object has two user-supplied attributes (In C++, “members”; in C#, “fields or properties or methods”; in Java, “fields or methods”). One of them is named data, and it stores the integer value 42. The other is named method, and it stores a function object. (There are several more attributes that Python adds automatically.)

    These attributes still aren’t really part of the object, though. Fundamentally, an object is just a bundle of more names (the attribute names), until you get down to things that can’t be divided up any more. Thus, values can be shared between different instances of a class, or even between objects of different classes, if you deliberately set that up.

    Let’s create an instance:

    x = Example()
    

    Now we have a separate object named x, which is an instance of Example. The data and method are not actually part of the object, but we can still look them up via x because of some magic that Python does behind the scenes. When we look up method, in particular, we will instead get a “bound method” (when we call it, x gets passed automatically as the self parameter, which cannot happen if we look up Example.method directly).

    What happens when we try to use x.data?

    When we examine it, it’s looked up in the object first. If it’s not found in the object, Python looks in the class.

    However, when we assign to x.data, Python will create an attribute on the object. It will not replace the class’ attribute.

    This allows us to do object initialization. Python will automatically call the class’ __init__ method on new instances when they are created, if present. In this method, we can simply assign to attributes to set initial values for that attribute on each object:

    class Example:
        name = "Ignored"
        def __init__(self, name):
            self.name = name
        # rest as before
    

    Now we must specify a name when we create an Example, and each instance has its own name. Python will ignore the class attribute Example.name whenever we look up the .name of an instance, because the instance’s attribute will be found first.

    One last caveat: modification (mutation) and assignment are different things!

    In Python, strings are immutable. They cannot be modified. When you do:

    a = 'hi '
    b = a
    a += 'mom'
    

    You do not change the original ‘hi ‘ string. That is impossible in Python. Instead, you create a new string 'hi mom', and cause a to stop being a name for 'hi ', and start being a name for 'hi mom' instead. We made b a name for 'hi ' as well, and after re-applying the a name, b is still a name for 'hi ', because 'hi ' still exists and has not been changed.

    But lists can be changed:

    a = [1, 2, 3]
    b = a
    a += [4]
    

    Now b is [1, 2, 3, 4] as well, because we made b a name for the same thing that a named, and then we changed that thing. We did not create a new list for a to name, because Python simply treats += differently for lists.

    This matters for objects because if you had a list as a class attribute, and used an instance to modify the list, then the change would be “seen” in all other instances. This is because (a) the data is actually part of the class object, and not any instance object; (b) because you were modifying the list and not doing a simple assignment, you did not create a new instance attribute hiding the class attribute.

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

Sidebar

Related Questions

I want to clarify whether different instances of an Objective-C class share static variables
Possible Duplicate: private members in python I've got few variables I really want to
Suppose you have a trigger on MY_CUSTOMER_TABLE and that it has a variable declared
I have several variables like: class X(object): ... class XY(X): ... class XZ(X): ...
I want to declare a variable which holds a class which implements a specific
I want to clarify refactoring in scope of TDD. Before: class Somclass{ public void
Want to make a confirm box appear before someone leave my site. They have
Want to play a file that on path like this /var/spool/sound/dog.wav present on server.
want to have a Hyperlink-Button in a gridView in which I can display a
I want to allow the users to define a formula based on two variables.

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.