On Fri, Apr 15, 2022 at 3:40 PM David Bailey <[email protected]> wrote:
>
> Thanks for such an amazingly fast and detailed reply!
>
> On 15/04/2022 21:32, Aaron Meurer wrote:
> >
> > Yes, you can have it separate. The location of a function in the code
> > has no bearing on its behavior.
> That is good!
> > When you run 'import bailey' then 'bailey' will be the module (because
> > you called your file bailey.py). To get the function do
> >
> > from bailey import bailey
>
> I tried that too, but I got:
>
>  >>> bailey(a+3)
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
>    File "C:\PythonSystem\lib\site-packages\sympy\core\cache.py", line
> 70, in wrapper
>      retval = cfunc(*args, **kwargs)
>    File "C:\PythonSystem\lib\site-packages\sympy\core\function.py", line
> 476, in __new__
>      result = super().__new__(cls, *args, **options)
>    File "C:\PythonSystem\lib\site-packages\sympy\core\cache.py", line
> 70, in wrapper
>      retval = cfunc(*args, **kwargs)
>    File "C:\PythonSystem\lib\site-packages\sympy\core\function.py", line
> 288, in __new__
>      evaluated = cls.eval(*args)
>    File "C:\SymPyProject\bailey.py", line 9, in eval
>      # Value at zero
> TypeError: 'Add' object is not subscriptable

You are calling arg[0], but arg is already the argument of the
function. The [0] is typically only used when you have *args to accept
an arbitrary number of arguments and you want to get the first one.

Aaron Meurer

>
> >
> >> Alternatively I tried:
> >>
> >>   >>> (a+3).bailey(1)
> >> Traceback (most recent call last):
> >>     File "<stdin>", line 1, in <module>
> >> AttributeError: 'Add' object has no attribute 'bailey'
> > You can't add new methods to existing SymPy objects. The first way you
> > tried is the correct way to access your bailey function.
> >
> >> I admit to being fairly confused at this level! What I would like to
> >> know is:
> >>
> >> 1)       Can I experiment in this way without actually adding files to
> >> SymPy itself or modifying anything there already?
> >>
> >> 2)       I would expect to get back a function call: bailey(a+3) or
> >> perhaps the answer 1.
> > The function as you've written it will just return 1. If you want a
> > symbolic answer, eval() needs to return None (which is also the same
> > as not returning anything).
> >
> > Since your eval() always returns something, the function will never be
> > symbolic. It is effectively no different from a normal Python function
> >
> > def bailey(x):
> >      if x == 0:
> >          return 0
> >      else:
> >          return 1
>
> To be absolutely clear, the actual content of the function is just junk
> at this point - I am just trying to get the basic mechanism to work.
>
> David
>
> --
> You received this message because you are subscribed to the Google Groups 
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sympy/2e6860c8-b1dd-fc37-9fa5-5bfa4d74b8d3%40dbailey.co.uk.

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/CAKgW%3D6LM1Oeu9oCRJnK5D-j5dyacskkRmiVNacrH9zOGZH1n-w%40mail.gmail.com.

Reply via email to