Python: How to pass more than one argument to the property getter?

python setter without getter
python setter multiple arguments
python property object
python property getter
python getter only property
python 2.7 property
python property fset
python add property

Consider the following example:

class A:
    @property
    def x(self): return 5

So, of course calling the a = A(); a.x will return 5

But imagine that you want to be able to modify the property x. This way, for example:

class A:
    @property
    def x(self, neg = False): return 5 if not neg else -5

And call it with a = A(); a.x(neg=True)

That will raise a TypeError: 'int' object is not callable, that is quite normal, since our x is evaluated as 5.

So, I would like to know how one can pass more then one argument to the property getter, if it is possible at all.

Note that you don't have to use property as a decorator. You can quite happily use it the old way and expose the individual methods in addition to the property:

class A:
    def get_x(self, neg=False):
        return -5 if neg else 5
    x = property(get_x)

>>> a = A()
>>> a.x
5
>>> a.get_x()
5
>>> a.get_x(True)
-5

This may or may not be a good idea depending on exactly what you're doing with it (but I'd expect to see an excellent justification in a comment if I came across this pattern in any code I was reviewing)

property getter with more than 1 argument? - Python, Is it possible to pass more than one argument to fget function? I know: I can define a function with property name ('newfun' in the example) and  Python: How to pass more than one argument to the property getter? (4) I know this question is old, but, for reference, you can call your property with an argument like that: a = A() assert a.x == 5 assert A.x.fget(a, True) == -5 As other mentioned by others, this is not advised.

I think you did not fully understand the purpose of properties.

If you create a property x, you'll accessing it using obj.x instead of obj.x(). After creating the property it's not easily possible to call the underlying function directly.

If you want to pass arguments, name your method get_x and do not make it a property:

def get_x(self, neg=False):
    return 5 if not neg else -5

If you want to create a setter, do it like this:

class A:
    @property
    def x(self): return 5

    @x.setter
    def x(self, value): self._x = value

Python Tutorial: Properties vs. getters and setters, Consider the following example: class A: @property def x(self): return 5 So, of course calling the a = A(); a.x will return 5 But imagine that you want to be able to​  newfun = property (fget, fset) >>a=FunDict() a.newfun=f1 a.newfun('f1') Traceback (most recent call last): File "<pyshell#67>", line 1, in <module> a.newfun('f1') TypeError: fget() takes exactly 2 arguments (1 given) Is it possible to pass more than one argument to fget function? I know: I can define a function with property name ('newfun' in the

a property should only depend on the related object. If you want to use some external parameters, you should use methods.

Python Tutorial: Passing Arguments, As soon as one of these programmers introduces a new attribute, he or she will it a private variable and creates "automatically" a getter and a setter for this attributes. that a property can be deduced from the values of more than one attribute. Parameter Passing in Functions · Namespaces · Global and Local Variables  Python Property () Function. In earlier versions of Python (<2.6), property decorators (which we will learn in a bit) were not introduced and property() function was used. The property() function is used to provide methods to control the access of attributes. Using property() function we can bind the getter,

In your second example, you're using a.x() as if it were a function: a.x(neg=True). With this in mind, why not just define it as a function?

Chapter 25, The passing of parameters and arguments in Python. pass-by-reference, a function gets an implicit reference to the argument, rather than a copy of its value. Unfortunately, it is widespread belief that a proper Python class should encapsulate private attributes by using getters and setters. As soon as one of these programmers introduces a new attribute, he or she will make it a private variable and creates "automatically" a getter and a setter for this attributes.

I know this question is old, but, for reference, you can call your property with an argument like that:

a = A()
assert a.x == 5
assert A.x.fget(a, True) == -5

As other mentioned by others, this is not advised.

Classes, This function accepts one argument and that argument has to be a function or We create one function and then pass it into a second function. Most of the time​, you will probably be doing your logging within the function Convert class methods into read-only attributes; Reimplement setters and getters into an attribute. Look, the "property" feature happens to be different from Java (Python's de facto nemesis for some reason), and therefore Python's community groupthink declares it to be better. In reality, properties violate the "Explicit is better than implicit" rule, but no one wants to admit it.

Python: How to pass more than one argument to the property getter?, In Python, everything is an object – everything is an instance of some class. inside the method bodies – but we don't appear to pass this parameter in. Setting arbitrary properties from outside the object is frowned upon even more, since it called getters and setters, because they “get” and “set” the values of attributes,  There is one more way to implement property function i.e. by using decorator. Python @property is one of the built-in decorators. Python @property is one of the built-in decorators. The main purpose of any decorator is to change your class methods or attributes in such a way so that the user of your class no need to make any change in their code.

Data classes - Classes, that is quite normal, since our x is evaluated as 5. So, I would like to know how one can pass more then one argument to the property getter, if it is possible at  Property is Different with attribute. The reason property used in encapsulation because python don't have built-in access modifier. In fact, when we call @property decorator above a method we instantiate a property object. Property object is an object have get,set, and del method. Property can be defined in a class without the need to define it in init.

17.1. threading — Thread-based parallelism, As opposed to Python, declaring a property directly inside the class does not create a Because the set of properties of an object is constrained to be exactly the set of A Kotlin class may have one primary constructor, whose parameters are the property declaration (and there can be at most one getter and one setter)  Fill in the foo and bar functions so they can receive a variable amount of arguments (3 or more) The foo function must return the amount of extra arguments received. The bar must return True if the argument with the keyword magicnumber is worth 7, and False otherwise.

Comments
  • Man... that is actually a brilliant idea. Wish more APIs used this instead of only setters-getters / only properties.
  • More decorators? They all support being used this way, as the decorator syntax is just sugar for a function call and name rebinding.
  • Oh, I see what you mean - more APIs exposing both the setter/getter methods and the related property.
  • I know! And you can see the a.x call in my first example.
  • @Rizo: properties are just not designed for that.
  • That's a direct solution, but i'm using very complex getters and setters and would like to keep using them without having to write separate functions for the attributes.
  • This does not answer my question. I now I can use functions in python! :) I just want to know if there some way to force a property to act as a function.
  • @Rizo why? If you want it to act like a function, then use a function (method). But in answer to your question, the only way would be to return a callable from the property that takes parameter. Then you would have something that looks exactly like a function but less efficient.
  • If a getter does anything but returning a simple value (or a default value) it's not a "getter" anymore but a regular function.
  • @Rizo: in the example you are giving, it is probably wiser to just let the caller decide what to do with the return value. after all it is the caller passing in the condition.