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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:07:48+00:00 2026-06-04T07:07:48+00:00

As the title says. Coming from Java im used to: private int A; public

  • 0

As the title says.

Coming from Java im used to:

private int A;

public void setA(int A) {
    this.A = A;
}

public int getA() {
    return this.A
}

How do I do that (if I need to) in Python.
And if one of __setattr__ or __set__ is used for this, what is the other one used for?

Edit:
I feel I need to clarify. I know that in Python one doe’s not create setters and getters before they are needed.

Lets say I want to do something like this:

public void setA(int A) {
    update_stuff(A);
    and_calculate_some_thing(A);
    this.A = A;
}

What is the “pythonic” way to implement this?

  • 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-04T07:07:49+00:00Added an answer on June 4, 2026 at 7:07 am

    In python, something like this should be implemented using a property (and then only when they do something useful).

    class Foo(object):
        def __init__(self):
            self._x = None
    
        @property
        def x(self):
            return self._x
    
        @x.setter
        def x(self,y):
            self._x = y
    

    In this example, it would be better to just do (as pointed out by Edward):

    class Foo(object):
         def __init__(self):
             self.x = None
    

    since our getter/setter methods don’t actually do anything … However, properties become very useful when the setter/getter actually does something more than just assign/return an attribute’s value.

    It could also be implemented using __setattr__/__getattr__ (but it shouldn’t be implemented this way as it quickly becomes cumbersome if your class has more than 1 property. I would also guess that doing it this way would be slower than using properties):

    class Foo(object):
        def __init__(self):
            self._x = None
        def __setattr__(self,attr,obj):
            if(attr == 'x'):
                object.__setattr__(self,'_x',obj)
            else:
                object.__setattr__(self,attr,obj)
    
        def __getattr__(self,attr):
            if(attr == 'x'):
                return object.__getattr__(self,'_x')
            else:
                return object.__getattr__(self,attr)
    

    In terms of what __setattr__ and __getattr__ actually do…
    __setattr__/__getattr__ are what are called when you do something like:

    myclassinstance = MyClass()
    myclassinstance.x = 'foo'  #translates to MyClass.__setattr__(myclassinstance,'x','foo')
    bar = myclassinstance.x    #translates to bar=MyClass.__getattr__(myclassinstance,'x')
    

    As for __get__ and __set__: previous posts have discussed that quite nicely.

    Note that in python there is no such thing as private variables. In general, in a class member is prefixed with an underscore, you shouldn’t mess with it (unless you know what you’re doing of course). If it’s prefixed with 2 underscores, it will invoke name-mangling which makes it harder to access outside the class. This is used to prevent namespace clashes in inheritance (and those variables are generally also not to be messed with).

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

Sidebar

Related Questions

As title says, mock code to demostrate my problem Driver Class import java.io.*; public
As title says i whant to change my variables in java from my jruby
As title says, how do I call a java class method from a jsp,
Title says all. I'll have navigation that looks like this and it's dynamically created
The title says it! I know that Jon Skeet was working on an implementation
As title says, is there any way to pull something like this up?: std::set<boost::shared_ptr<MyClass>>
As the title says i want to call a mixed mode assembly from unmanaged
As title says, this issue happens in MS Access 2003 SP1. Does anyone know
The title says everything. I am talking about C/C++ specifically, because both consider this
As title says, i need to get this information regarding the screen state. I

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.