基于实例变量值实现类方法(面向对象编程)

huangapple go评论85阅读模式
英文:

Implement class method based on instance variable values (OOP)

问题

I am new to OOP. I want to create objects that have a method which depends on the instance variables value.

  1. class cases:
  2. def __init__(self, a):
  3. self.a = a
  4. def f1(self, x):
  5. return x
  6. def f2(self, x):
  7. return 100*x
  8. if self.a < 3:
  9. self.x = f1
  10. else:
  11. self.x = f2

If I create the following instances c1 and c2 by the class cases:

  1. c1 = cases(2)
  2. c2 = cases(5)

and execute the method x() I expect that:

  1. c1.x(10) # --> 10
  2. c2.x(10) # --> 1000

But right now the error is:

  1. NameError: name 'self' is not defined

If I change self.a to just a in the class definition, the error is:

  1. NameError: name 'a' is not defined

So how can I access the variable instance for a case discrimination?

UPDATE
By following the instructions of @deceze (2nd try). Code becomes as follows:

  1. def f1(x):
  2. return x
  3. def f2(x):
  4. return 100*x
  5. class case:
  6. def __init(self, a):
  7. self.a = a
  8. def x(self, x):
  9. if self.a < 3:
  10. return f1(x)
  11. else:
  12. return f2(x)
  13. # make instances with different cases
  14. c1 = case(2)
  15. c2 = case(4)
  16. # show results
  17. print(c1.x(10))
  18. print(c2.x(10))

Result is as desired.

英文:

I am new to OOP. I want to create objects that have a method which depends on the instance variables value.

  1. class cases:
  2. def __init__(self, a):
  3. self.a = a
  4. def f1(self, x):
  5. return x
  6. def f2(self, x):
  7. return 100*x
  8. if self.a < 3:
  9. self.x = f1
  10. else:
  11. self.x = f2

If I create the following instances c1 and c2 by the class cases:

  1. c1 = cases(2)
  2. c2 = cases(5)

and execute the method x() I expect that:

  1. c1.x(10) # --> 10
  2. c2.x(10) # --> 1000

But right now the error is:

  1. NameError: name 'self' is not defined

If I change self.a to just a in the class definition, the error is:

  1. NameError: name 'a' is not defined

So how can I access the variable instance for a case discrimination?


UPDATE
By following the instructions of @deceze (2nd try). Code becomes the followed:

  1. def f1(x):
  2. return x
  3. def f2(x):
  4. return 100*x
  5. class case:
  6. def __init__(self, a):
  7. self.a = a
  8. def x(self, x):
  9. if self.a < 3:
  10. return f1(x)
  11. else:
  12. return f2(x)
  13. # make instances with different cases
  14. c1 = case(2)
  15. c2 = case(4)
  16. # show results
  17. print(c1.x(10))
  18. print(c2.x(10))

Result is as desired.

答案1

得分: 2

You are mixing up instances and parameters.

  1. def f1(x):
  2. return x
  3. def f2(x):
  4. return 100 * x
  5. class case:
  6. def __init__(self, a):
  7. self.a = a
  8. def x(self, a):
  9. if self.a < 3:
  10. return f1(a)
  11. else:
  12. return f2(a)
  13. # make instances with different cases
  14. c1 = case(2)
  15. c2 = case(4)
  16. # show results
  17. print(c1.x(10))
  18. print(c2.x(10))

Output as expected.

英文:

You are mixing up instances and parameters.

  1. def f1(x):
  2. return x
  3. def f2(x):
  4. return 100*x
  5. class case:
  6. def __init__(self, a):
  7. self.a = a
  8. def x(self, a):
  9. if self.a &lt; 3:
  10. return f1(a)
  11. else:
  12. return f2(a)
  13. # make instances with different cases
  14. c1 = case(2)
  15. c2 = case(4)
  16. # show results
  17. print(c1.x(10))
  18. print(c2.x(10))

Output as expected.

huangapple
  • 本文由 发表于 2023年4月4日 17:10:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75927524.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定