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

  • Home
  • SEARCH
  • 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 992199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:13:42+00:00 2026-05-16T06:13:42+00:00

I understand why Python requires explicit self qualifier when referring to instance attributes. But

  • 0

I understand why Python requires explicit self qualifier when referring to instance attributes.

But I often forget it, since I didn’t need it in C++.

The bug I introduce this way is sometimes extremely hard to catch; e.g., suppose I write

if x is not None:
    f()

instead of

if self.x is not None:
    f()

Suppose attribute x is usually None, so f() is rarely called. And suppose f() only creates a subtle side effect (e.g., a change in a numeric value, or clearing the cache, etc.). Unless I have insane amount of unit tests, this mistake is likely to remain unnoticed for a long time.

I am wondering if anyone knows coding techniques or IDE features that could help me catch or avoid this type of bug.

  • 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-16T06:13:43+00:00Added an answer on May 16, 2026 at 6:13 am

    Don’t name your instance attributes the same things as your globals/locals.

    If there isn’t a global/local of the same name, you’ll get a global "foo" is not defined error when you try to access self.foo but forget the self..

    As a corollary: give your variables descriptive names. Don’t name everything x – not only does this make it far less likely you’ll have variables with the same name as attributes, it also makes your code easier to read.

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

Sidebar

Related Questions

I've been trying to understand Python's handling of class and instance variables. In particular,
I'm a newcomer to Python but I understand that things should not be done
I'm having terrible trouble trying to understand python scoping rules. With the following script:
I'm trying to understand how python 2.5 deals with unicode strings. Although by now
I understand Ruby and Python's yield. What does Scala's yield do?
I try to understand the regex in python. How can i split the following
Very new to python and can't understand why this isn't working. I have a
I am trying to understand this simple hashlib code in Python that has been
I'm trying to understand an estrange behavior in Python. I have the next python
I would like to understand how object deletion works on python. Here is a

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.