Are python attribute getters useful?


squid

I wonder if there is any use of explicitly using getter decorators for class properties:

class A:
    @property
    def p(self):
       return self._p

    @p.setter
    def p(self, val):
       assert p < 1000
       self._p = val

    @p.getter
    def p(self):
       return self._p

Why would I explicitly use a getter here? Does it make the code in p(self) inaccessible? Basically, is there any practical reason to use it?

Alan Fee

This is useful when you inherit a property from a parent class and want to override the getter:

class Parent:
    @property
    def prop(self):
        return 'Parent'

    @prop.setter
    def prop(self, value):
        print('prop is now', value)


class Child(Parent):
    @Parent.prop.getter
    def prop(self):
        return 'Child'

This creates a copy Foo.propwith a different getter function but the same setter (and deleter) .

Related


Are python attribute getters useful?

squid I wonder if there is any use of explicitly using getter decorators for class properties: class A: @property def p(self): return self._p @p.setter def p(self, val): assert p < 1000 self._p = val @p.getter def

Are python attribute getters useful?

squid I wonder if there is any use of explicitly using getter decorators for class properties: class A: @property def p(self): return self._p @p.setter def p(self, val): assert p < 1000 self._p = val @p.getter def

Instance attribute getters in Python

Ruud Verhoff I try to write custom getter methods for class properties. But I can't find the correct syntax in Python. I tried what was mentioned in this question , but using a @propertydecorator doesn't seem to work, or I have the wrong name. kind: class Comm

Instance attribute getters in Python

Ruud Verhoff I try to write custom getter methods for class properties. But I can't find the correct syntax in Python. I tried what was mentioned in this question , but using a @propertydecorator doesn't seem to work, or I have the wrong name. kind: class Comm

Instance attribute getters in Python

Ruud Verhoff I try to write custom getter methods for class properties. But I can't find the correct syntax in Python. I tried what was mentioned in this question , but using a @propertydecorator doesn't seem to work, or I have the wrong name. kind: class Comm

Instance attribute getters in Python

Ruud Verhoff I try to write custom getter methods for class properties. But I can't find the correct syntax in Python. I tried what was mentioned in this question , but using a @propertydecorator doesn't seem to work, or I have the wrong name. kind: class Comm

Instance attribute getters in Python

Ruud Verhoff I try to write custom getter methods for class properties. But I can't find the correct syntax in Python. I tried what was mentioned in this question , but using a @propertydecorator doesn't seem to work, or I have the wrong name. kind: class Comm

Instance attribute getters in Python

Ruud Verhoff I try to write custom getter methods for class properties. But I can't find the correct syntax in Python. I tried what was mentioned in this question , but using a @propertydecorator doesn't seem to work, or I have the wrong name. kind: class Comm

File input 'accept' attribute - useful?

Darren Oster Implementing file uploads under html is pretty straightforward, but I just noticed that the "accept" attribute can be added to the <input type="file" ...>tag. Is this property useful as a way to restrict file uploads to images etc? What's the best

File input 'accept' attribute - useful?

Darren Oster Implementing file uploads under html is pretty straightforward, but I just noticed that the "accept" attribute can be added to the <input type="file" ...>tag. Is this property useful as a way to restrict file uploads to images etc? What's the best

Getters and Setters in Python

Thank you I'm completely new to Python and I'm trying to write a class that will just store two properties, one intended to be used as a number and the other intended to be used as a datetime object. Although I also want to initialize and output with a string

Python, Getters in Tkinter

bazzuk123 I need to access information from my "makeEntry" class which happens to be textvariables. I tried the make get function, but I read in python that it is not necessary. def temp(): print(e_full_name.get_text()) class makeEnetry: def __init__(s

Getters and Setters in Python

Thank you I'm completely new to Python and I'm trying to write a class that will just store two properties, one intended to be used as a number and the other intended to be used as a datetime object. Although I also want to initialize and output with a string

Python, Getters in Tkinter

bazzuk123 I need to access information from my "makeEntry" class which happens to be textvariables. I tried the make get function, but I read in python that it is not necessary. def temp(): print(e_full_name.get_text()) class makeEnetry: def __init__(s

Python, Getters in Tkinter

bazzuk123 I need to access information from my "makeEntry" class which happens to be textvariables. I tried the make get function, but I read in python that it is not necessary. def temp(): print(e_full_name.get_text()) class makeEnetry: def __init__(s

Python, Getters in Tkinter

bazzuk123 I need to access information from my "makeEntry" class which happens to be textvariables. I tried the make get function, but I read in python that it is not necessary. def temp(): print(e_full_name.get_text()) class makeEnetry: def __init__(s

Getters and Setters in Python

Thank you I'm completely new to Python and I'm trying to write a class that will just store two properties, one intended to be used as a number and the other intended to be used as a datetime object. Although I also want to initialize and output with a string

Getters and Setters in Python

Thank you I'm completely new to Python and I'm trying to write a class that will just store two properties, one intended to be used as a number and the other intended to be used as a datetime object. Although I also want to initialize and output with a string

Python, Getters in Tkinter

bazzuk123 I need to access information from my "makeEntry" class which happens to be textvariables. I tried the make get function, but I read in python that it is not necessary. def temp(): print(e_full_name.get_text()) class makeEnetry: def __init__(s

Python, Getters in Tkinter

bazzuk123 I need to access information from my "makeEntry" class which happens to be textvariables. I tried the make get function, but I read in python that it is not necessary. def temp(): print(e_full_name.get_text()) class makeEnetry: def __init__(s

Python, Getters in Tkinter

bazzuk123 I need to access information from my "makeEntry" class which happens to be textvariables. I tried the make get function, but I read in python that it is not necessary. def temp(): print(e_full_name.get_text()) class makeEnetry: def __init__(s

When is del useful in python?

Jason Baker: I can't really think of any reason why python would need the delkeyword (most languages don't seem to have a similar keyword). For example, instead of deleting a variable, you can delete a variable None. A method can be added when removing from th

Why are Python Lambdas useful?

Mead: I am trying to figure out Python lambdas. Is lambda one of those "interesting" language projects that should be forgotten in real life? I'm sure it may be necessary to use it in some cases, but given its obscurity, it's potential to be redefined in futur

When is del useful in python?

Jason Baker: I can't really think of any reason why python would need the delkeyword (most languages don't seem to have a similar keyword). For example, instead of deleting a variable, you can delete a variable None. A method can be added when removing from th

Why are Python Lambdas useful?

Mead: I am trying to figure out Python lambdas. Is lambda one of those "interesting" language projects that should be forgotten in real life? I'm sure it may be necessary to use it in some cases, but given its obscurity, the potential to redefine it in future

Is _tuple useful in Python?

Hahahaha I read the official documentation today and found the method. I didn't find where to define it.collections.namedtuple_tuple__new___tuple You can try running the following code in Python, it doesn't throw any errors. >>> Point = namedtuple('Point', ['x