AttributeError: can't set attribute when trying to increment X value of a circle in sympy[2] I want 3Pattern
AttributeError: can't set attribute when trying to increment X value of a circle in sympy[2] I want 3Pattern
我要3个图案①②➂
https://docs.sympy.org/latest/modules/geometry/points.html#sympy.geometry.point.Point2D.x
from sympy import Point, Circle
def myPointSubs(PT,x,y):
return PT.translate(x=-PT.x+x,y=-PT.y+y)
test_center=Point (1,2)
test_circle=Circle(myPointSubs(test_center,2,test_center.y), 1)
# test_circle=Circle(myPointSubs(test_center,2,), 1) #① I want Circle(Point2D(2,2), 1)
# test_circle=Circle(myPointSubs(test_center,,1), 1) #② I want Circle(Point2D(1,1), 1)
# test_circle=Circle(myPointSubs(test_center,, ), 1) #➂ I want Circle(Point2D(1,2), 1)
print("#",test_circle)
# Circle(Point2D(2, 2), 1)
test_circle=Circle(myPointSubs(test_center,2,), 1)
TypeError:myPointSubs() 缺少 1 个必需的位置参数:'y'
(2021-12-28)
from sympy import Point
def change(pt,x=None,y=None):
return pt.func(pt.x if x is None else x, pt.y if y is None else y)
print("#",change(Point(1, 2), y=3))
print("#",change(Point(1, 2), x=3))
print("#",change(Point(1, 2), 3))
print("#",change(Point(1, 2), 2, 1))
print("")
print("#1",change(Point(1, 2) ))
# print("#2",change(Point(1, 2),, )) #SyntaxError: invalid syntax
# print("#3",change(Point(1, 2),,4)) #SyntaxError: invalid syntax
print("#4",change(Point(1, 2), x=3,y=4))
print("#5",change(Point(1, 2), y=4,x=3))
print("#6",change(Point(1, 2),3,))
# Point2D(1, 3)
# Point2D(3, 2)
# Point2D(3, 2)
# Point2D(2, 1)
#1 Point2D(1, 2)
# print("#2",change(Point(1, 2),, )) #SyntaxError: invalid syntax
# ^
# SyntaxError: invalid syntax
# print("#3",change(Point(1, 2),,4)) #SyntaxError: invalid syntax
# ^
# SyntaxError: invalid syntax
#4 Point2D(3, 4)
#5 Point2D(3, 4)
#6 Point2D(3, 2)
google > python none 默认参数
from sympy import *
def change(pt,x=None,y=None):
return pt.func(pt.x if x is None else x, pt.y if y is None else y)
test_center =Point (1,2)
test_circle =Circle(change(test_center,2,test_center.y), 1)
test_circle1=Circle(change(test_center,x=2),1) #① I want Circle(Point2D(2,2), 1)
test_circle2=Circle(change(test_center,y=1),1) #② I want Circle(Point2D(1,1), 1)
test_circle3=Circle(change(test_center ),1) #➂ I want Circle(Point2D(1,2), 1)
print("# ",test_circle )
print("#1",test_circle1)
print("#2",test_circle2)
print("#3",test_circle3)
# Circle(Point2D(2, 2), 1)
#1 Circle(Point2D(2, 2), 1)
#2 Circle(Point2D(1, 1), 1)
#3 Circle(Point2D(1, 2), 1)
处理可选参数的一种方法是给它们一个默认的 None 值,然后在它们 None 或不在函数中时处理它们:
>>> from sympy import Point
>>> def change(pt,x=None,y=None):
... return pt.func(pt.x if x is None else x, pt.y if y is None else y)
...
>>> change(Point(1,2),y=3)
Point2D(1, 3)
>>> change(Point(1,2),x=3)
Point2D(3, 2)
>>> change(Point(1,2),3)
>>> change(Point(1,2),2,1)
Point2D(2, 1)
你得到的错误应该是(回想起来)不言自明的:函数需要一个你没有提供的参数:它需要 3 个参数而你给了它 2 个。在我定义的函数中,通过定义默认参数值,当我不给它所有的参数时它不会抱怨......但是如果我想改变 y 而不是 x,我不能只使用 pt,,yval
;我必须将 yval
传递为 y=yval
。
我要3个图案①②➂
https://docs.sympy.org/latest/modules/geometry/points.html#sympy.geometry.point.Point2D.x
from sympy import Point, Circle
def myPointSubs(PT,x,y):
return PT.translate(x=-PT.x+x,y=-PT.y+y)
test_center=Point (1,2)
test_circle=Circle(myPointSubs(test_center,2,test_center.y), 1)
# test_circle=Circle(myPointSubs(test_center,2,), 1) #① I want Circle(Point2D(2,2), 1)
# test_circle=Circle(myPointSubs(test_center,,1), 1) #② I want Circle(Point2D(1,1), 1)
# test_circle=Circle(myPointSubs(test_center,, ), 1) #➂ I want Circle(Point2D(1,2), 1)
print("#",test_circle)
# Circle(Point2D(2, 2), 1)
test_circle=Circle(myPointSubs(test_center,2,), 1)
TypeError:myPointSubs() 缺少 1 个必需的位置参数:'y'
(2021-12-28)
from sympy import Point
def change(pt,x=None,y=None):
return pt.func(pt.x if x is None else x, pt.y if y is None else y)
print("#",change(Point(1, 2), y=3))
print("#",change(Point(1, 2), x=3))
print("#",change(Point(1, 2), 3))
print("#",change(Point(1, 2), 2, 1))
print("")
print("#1",change(Point(1, 2) ))
# print("#2",change(Point(1, 2),, )) #SyntaxError: invalid syntax
# print("#3",change(Point(1, 2),,4)) #SyntaxError: invalid syntax
print("#4",change(Point(1, 2), x=3,y=4))
print("#5",change(Point(1, 2), y=4,x=3))
print("#6",change(Point(1, 2),3,))
# Point2D(1, 3)
# Point2D(3, 2)
# Point2D(3, 2)
# Point2D(2, 1)
#1 Point2D(1, 2)
# print("#2",change(Point(1, 2),, )) #SyntaxError: invalid syntax
# ^
# SyntaxError: invalid syntax
# print("#3",change(Point(1, 2),,4)) #SyntaxError: invalid syntax
# ^
# SyntaxError: invalid syntax
#4 Point2D(3, 4)
#5 Point2D(3, 4)
#6 Point2D(3, 2)
google > python none 默认参数
from sympy import *
def change(pt,x=None,y=None):
return pt.func(pt.x if x is None else x, pt.y if y is None else y)
test_center =Point (1,2)
test_circle =Circle(change(test_center,2,test_center.y), 1)
test_circle1=Circle(change(test_center,x=2),1) #① I want Circle(Point2D(2,2), 1)
test_circle2=Circle(change(test_center,y=1),1) #② I want Circle(Point2D(1,1), 1)
test_circle3=Circle(change(test_center ),1) #➂ I want Circle(Point2D(1,2), 1)
print("# ",test_circle )
print("#1",test_circle1)
print("#2",test_circle2)
print("#3",test_circle3)
# Circle(Point2D(2, 2), 1)
#1 Circle(Point2D(2, 2), 1)
#2 Circle(Point2D(1, 1), 1)
#3 Circle(Point2D(1, 2), 1)
处理可选参数的一种方法是给它们一个默认的 None 值,然后在它们 None 或不在函数中时处理它们:
>>> from sympy import Point
>>> def change(pt,x=None,y=None):
... return pt.func(pt.x if x is None else x, pt.y if y is None else y)
...
>>> change(Point(1,2),y=3)
Point2D(1, 3)
>>> change(Point(1,2),x=3)
Point2D(3, 2)
>>> change(Point(1,2),3)
>>> change(Point(1,2),2,1)
Point2D(2, 1)
你得到的错误应该是(回想起来)不言自明的:函数需要一个你没有提供的参数:它需要 3 个参数而你给了它 2 个。在我定义的函数中,通过定义默认参数值,当我不给它所有的参数时它不会抱怨......但是如果我想改变 y 而不是 x,我不能只使用 pt,,yval
;我必须将 yval
传递为 y=yval
。