简体   繁体   English

Python面向对象的实现

[英]Python object oriented implementation

Im trying this piece of code below but wondering why its not working.. 我在下面尝试这段代码,但想知道为什么它不起作用。

Can you please let me know wht is the problem, 你能告诉我是什么问题吗

code.py code.py

class Example():


    def A1(self):
        return self.B1() + self.B2()


    def B1(self):
        return 4 * self.C1() 

    def B2(self):
        return 5

    def C1(self):
        return 2


def main():
    spreadsheet = Example()
    print spreadsheet.A1()

    spreadsheet.C1 = 3
    print spreadsheet.A1()

C1 starts out as a method - you call .A1(), which calls .B1(), which calls .C1(), which returns 2. So far, so good. C1作为一种方法开始-您调用.A1(),然后调用.B1(),然后再调用.C1(),然后返回2。到目前为止,还不错。

You then make .C1 a value (3) - you call .A1(), which calls .B1(), which tries to call .C1(), which blows up because the value 3 is not callable . 然后,将.C1设置为值(3)-调用.A1(),后者调用.B1(),后者试图调用.C1(),后者由于值3不可调用而被炸毁。

Maybe you want something like 也许你想要类似的东西

class Spreadsheet(object):
    def __getattribute__(self, cell):
        val = object.__getattribute__(self, cell)
        return val(self) if callable(val) else val

def main():
    s = Spreadsheet()

    s.a1 = lambda self: self.b1 + self.b2
    s.b1 = lambda self: 4 * self.c1
    s.b2 = 5

    s.c1 = 2
    print s.a1  # => 13

    s.c1 = 3
    print s.a1  # => 17

In your class, the variable C1 was just a variable that held an instance method. 在您的类中,变量C1只是一个保存实例方法的变量。 Setting it equal to 3 overrides the function. 将其设置为等于3覆盖该功能。

For example: 例如:

def foo():
  print 4

foo = 12  # foo is now the number 12, not a function

You did the same thing: 您做了同样的事情:

spreadsheet.C1 = 3

C1 was a function, but now it's a number. C1 一个函数,但现在是一个数字。 You can't call a number. 您无法拨打电话。

The name "C1" refers to a method (a callable object). 名称“ C1”是指方法(可调用对象)。 But then you assign "C1" attribute to an integer (not callable), and clobber the method. 但是,然后将“ C1”属性分配给整数(不可调用),并破坏该方法。 Then when you call it with self.C1() it won't work anymore. 然后,当您使用self.C1()对其进行调用时,它将不再起作用。

You mean that if you execute main you get a TypeError when it gets to print spreadsheet.A1() 您的意思是,如果执行main,则在打印电子表格时会遇到TypeError.A1()

It is because you overwrite the instance variable C1. 这是因为您覆盖了实例变量C1。 It was assigned to a function, but then you reassign it to the integer 3. When it tries to process the instance method A1 it eventually tries to call self.C1() and finds an integer, which can't be called and shouldn't have the parenthesis after it, so it properly throws an error. 它已分配给一个函数,但随后将其重新分配给整数3。当它尝试处理实例方法A1时,最终尝试调用self.C1()并找到一个整数,该整数不能被调用并且不应t后面没有括号,因此它会引发错误。

Depending on what you want to happen you have several options. 根据您要发生的事情,您有几种选择。

  • Accept that this is normal behavior and don't overwrite instance procedures. 接受这是正常行为,不要覆盖实例过程。
  • Define a setter for C1 that would either return an error at that point or silently do nothing. 为C1定义一个设置器,该设置器将在此时返回错误或不执行任何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM