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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:38:06+00:00 2026-05-14T15:38:06+00:00

I recently discovered Python’s property built-in , which disguises class method getters and setters

  • 0

I recently discovered Python’s property built-in, which disguises class method getters and setters as a class’s property. I’m now being tempted to use it in ways that I’m pretty sure are inappropriate.

Using the property keyword is clearly the right thing to do if class A has a property _x whose allowable values you want to restrict; i.e., it would replace the getX() and setX() construction one might write in C++.

But where else is it appropriate to make a function a property? For example, if you have

class Vertex(object):
    def __init__(self):
        self.x = 0.0
        self.y = 1.0

class Polygon(object):
    def __init__(self, list_of_vertices):
        self.vertices = list_of_vertices
    def get_vertex_positions(self):
        return zip( *( (v.x,v.y) for v in self.vertices ) )

is it appropriate to add

    vertex_positions = property( get_vertex_positions )

?

Is it ever ok to make a generator look like a property? Imagine if a change in our code meant that we no longer stored Polygon.vertices the same way. Would it then be ok to add this to Polygon?

    @property
    def vertices(self):
        for v in self._new_v_thing:
            yield v.calculate_equivalent_vertex()
  • 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-14T15:38:06+00:00Added an answer on May 14, 2026 at 3:38 pm
    • When you have a normal attribute and getting and/or setting it makes sense for a class’s user, expose the attribute directly. One big reason that public members are anathema in some languages is that if you need to do something more complex later you would need an API change; in Python you can just define a property.

    • If you are using something you should abstract as attribute access, use property. If you want to make external state (your plot or website or something) aware of the change or if you are wrapping some library that uses members directly, properties might be the way to go.

    • If something isn’t attribute-y, don’t make it a property. There’s no harm in making a method, and it can be advantageous: what it does is more obvious, you can pass around some bound method if you need, you can add keyword arguments without an API change.

      It’s hard to imagine a situation where I would use a generator function as a property. The only way to have a normal attribute that would behave at all similarly would require quite a lot of complexity, so that case isn’t very reminiscent of attribute access.

    • You point out you can use property for restricting access to some internal attribute _x. This can be true, but keep in mind

      • If you are going to do things like sanitize input for security or something important, explicit is better than implicit. You don’t want to feel like code just-works when it comes to such things, because then you’ll run into code that doesn’t.

      • Sometimes people use property to implement read-only attributes. It’s usually better just to have a normal attribute and realize you can’t stop a user from doing something dumb and unsupported.

    • Nitpicking you might find interesting:

      • property isn’t a keyword; it’s a normal name you can rebind. This is sort of
        interesting since property isn’t a syntax thing or anything: it’s a normal class you could implement yourself in pure Python. It uses the same mechanism that makes methods work in Python—descriptors.

      • You describe what property does as “disguises class method getters and setters”, which isn’t quite the case. The things that property take are just normal functions, and needn’t actually be defined in your class at all; property will pass self for you. Functions don’t actually become methods until you look them up, when Python creates method objects on-the-fly. During the class definition, they are just functions. When you have a normal method it’s called an “instance method”; in Python “class method” refers to another special thing sort of like properties that changes what happens when an attribute is looked up.

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

Sidebar

Related Questions

I recently discovered metaclasses in python. Basically a metaclass in python is a class
I was recently teaching myself Python and discovered the LBYL/EAFP idioms with regards to
Having recently discovered this method of development, I'm finding it a rather nice methodology.
I recently discovered Erlang and am now working my way through a couple of
We recently discovered that the Google Maps API does not play nicely with SSL.
I recently discovered the genshi.builder module. It reminds me of Divmod Nevow's Stan module.
I only just recently discovered that Visual C++ 2008 (and perhaps earlier versions as
I've recently discovered the IsolatedStorage facilities in .net, and I'm wondering when I should
Recently I have discovered that my release executable (made with msvc++ express 2008) becomes
I've discovered this idiom recently, and I am wondering if there is something 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.