English 中文(简体)
按财产功能顺序排列的 Python误
原标题:Python error with order of property function

出于某种原因,当我围绕财产功能的争论而转变时,它就留下了一个错误。 我只想理解为什么。

class Geeks:
    def __init__(self):
        self._age = 0

    # function to get value of _age
    def get_age(self):
        print("getter method called")
        return self._age

    # function to set value of _age
    def set_age(self, a):
        print("setter method called")
        self._age = a

    age = property(get_age, set_age)

mark = Geeks()
mark.age = 10
print(mark.age)

产出:

setter method called
getter method called
10

错误发生时,我转接了<代码> = 财产(植被、定型_age)

class Geeks:
    def __init__(self):
        self._age = 0

    # function to get value of _age
    def get_age(self):
        print("getter method called")
        return self._age

    # function to set value of _age
    def set_age(self, a):
        print("setter method called")
        self._age = a

    age = property(set_age, get_age)

mark = Geeks()
mark.age = 10
print(mark.age)

产出

ERROR!
Traceback (most recent call last):
  File "<string>", line 18, in <module>
TypeError: Geeks.get_age() takes 1 positional argument but 2 were given

Why am I not allowed to switch around the arguments?

问题回答

在有问题的<条码>property电话中,你有错的机组和跳板。 在没有代号参数的情况下,Sharma将坚持要求(沉积)的命令。

如果你重新使用positional参数(并且你是),那么,采集器应当首先使用。 既然你挥舞了他们,那么这两个论点都将是错误的,因为<>><>><>>>>>>。 依据的事实是:

  • a getter is a function that takes no arguments (other than self) and returns a value; and
  • a setter is defined as a function that takes one argument (again, other than self) and returns nothing.

如果你真的想要他们,你需要 姓名<>。 他们明确表明,斯图尔将适应你的命令:

age = property(fset=set_age, fget=get_age)

但是,你希望考虑的一件事是使用财产校正器,使其更明确地说明为此目的的职能。 对您来说,这将带来这样的情况:

@property
def age(self):
    return self._age
    
@age.setter
def age(self, val):
    self._age = val




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签