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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:34:28+00:00 2026-05-13T07:34:28+00:00

I have been learning Python by following some pygame tutorials . Therein I found

  • 0

I have been learning Python by following some pygame tutorials.

Therein I found extensive use of the keyword self, and coming from a primarily Java background, I find that I keep forgetting to type self. For example, instead of self.rect.centerx I would type rect.centerx, because, to me, rect is already a member variable of the class.

The Java parallel I can think of for this situation is having to prefix all references to member variables with this.

Am I stuck prefixing all member variables with self, or is there a way to declare them that would allow me to avoid having to do so?

Even if what I am suggesting isn’t pythonic, I’d still like to know if it is possible.

I have taken a look at these related SO questions, but they don’t quite answer what I am after:

  • Python – why use “self” in a class?
  • Why do you need explicitly have the “self” argument into a Python method?
  • 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-13T07:34:28+00:00Added an answer on May 13, 2026 at 7:34 am

    In Java terms: Python doesn’t have member functions, all class functions are static, and are called with a reference to the actual class instance as first argument when invoked as member function.

    This means that when your code has a class MyClass and you build an instance m = MyClass(), calling m.do_something() will be executed as MyClass.do_something(m).

    Also note that this first argument can technically be called anything you want, but the convention is to use self, and you should stick to that convention if you want others (including your future self) to be able to easily read your code.

    The result is there’s never any confusion over what’s a member and what’s not, even without the full class definition visible. This leads to useful properties, such as: you can’t add members which accidentally shadow non-members and thereby break code.

    One extreme example: you can write a class without any knowledge of what base classes it might have, and always know whether you are accessing a member or not:

    class A(some_function()):
      def f(self):
        self.member = 42
        self.method()
    

    That’s the complete code! (some_function returns the type used as a base.)

    Another, where the methods of a class are dynamically composed:

    class B(object):
      pass
    
    print B()
    # <__main__.B object at 0xb7e4082c>
    
    def B_init(self):
      self.answer = 42
    def B_str(self):
      return "<The answer is %s.>" % self.answer
    # notice these functions require no knowledge of the actual class
    # how hard are they to read and realize that "members" are used?
    
    B.__init__ = B_init
    B.__str__ = B_str
    
    print B()
    # <The answer is 42.>
    

    Remember, both of these examples are extreme and you won’t see them every day, nor am I suggesting you should often write code like this, but they do clearly show aspects of self being explicitly required.

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

Sidebar

Related Questions

I have been learning python for some time now. While starting this learning python
I'm learning Python (with Python 3.2) and have been following a few manuals. In
I have been learning to use sqlite3 with python. Right now, I have a
I have been learning how to use the Python multiprocessing module recently, and reading
I have been learning Python for a while, and now I'd like to learn
I have been learning a bit of Python 2 and Python 3 and it
I'm learning Python and I have been playing around with packages. I wanted to
I am learning Python and building my first web app. I have been going
I have very recently been learning python. I have been using pyscripter as is
I'm learning Python and have been trying to understand more about the details of

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.