Python中调用property()函数时出现SyntaxError: keyword argument repeated: fget错误的技术问询
SyntaxError: keyword argument repeated: fget in Your Python Property Code Hey there! Let's break down what's going wrong here and get your Rectangle class working properly.
Why You're Seeing This Error
The immediate issue is that you're passing the fget and fset keyword arguments twice in your property() call:
Dimensions = property(fget = _get_Height, fset = _set_Height, fget = _get_Width, fset = _set_Width)
Python doesn't allow duplicate keyword arguments in a function call, which triggers the SyntaxError you're seeing.
Beyond that, there's a logical mistake: a single property object is designed to manage one attribute at a time. You can't use one property to handle both Height and Width—each needs its own property definition.
How to Fix It
There are two clean ways to implement properties for your Rectangle class.
Option 1: Use Separate property() Calls (Traditional Approach)
Define individual properties for height and width, each with their own getter and setter methods. We'll also use the Python convention of prefixing internal storage attributes with an underscore (_) to signal they're not meant to be accessed directly:
class Rectangle: def __init__(self, height, width): self._height = height self._width = width # Getter and setter for height def _get_height(self): return self._height def _set_height(self, value): self._height = value height = property(fget=_get_height, fset=_set_height) # Getter and setter for width def _get_width(self): return self._width def _set_width(self, value): self._width = value width = property(fget=_get_width, fset=_set_width)
Option 2: Use @property Decorators (More Pythonic)
The @property decorator syntax is cleaner and more readable for defining properties in Python. It eliminates the need to explicitly call property() and ties getters/setters directly to their attribute names:
class Rectangle: def __init__(self, height, width): self._height = height self._width = width @property def height(self): return self._height @height.setter def height(self, value): self._height = value @property def width(self): return self._width @width.setter def width(self, value): self._width = value
Bonus: Add a Combined Dimensions Property (If Needed)
If you wanted a single attribute to get/set both dimensions at once (instead of using two separate properties), you can define a dedicated dimensions property:
class Rectangle: def __init__(self, height, width): self._height = height self._width = width @property def dimensions(self): return (self._height, self._width) @dimensions.setter def dimensions(self, values): # Add validation to ensure we get valid input if isinstance(values, (list, tuple)) and len(values) == 2: self._height, self._width = values else: raise ValueError("Dimensions must be a list or tuple containing two numbers")
Now you can use it like this:
rect = Rectangle(5, 10) print(rect.dimensions) # Output: (5, 10) rect.dimensions = (15, 20) print(rect.height) # Output: 15
内容的提问来源于stack exchange,提问作者Martin Kamau




