'function' object has no attribute '__code__'

Hello everyone,

I need an object to dynamically determine how many arguments a function possesses.

The usual way I would do it, as in accessing the function’s __ code __ attribute, seems to result in an attribute error.

Here is an example:

foo = lambda x, y=2, z=3: x+y+z
print(foo.__code__.argcount)

While in a standard Python environment, this will return the correct amount of arguments, on Anvil it raises the following error:

AttributeError: 'function' object has no attribute '__code__'

Am I doing something wrong?

On the server side you have standard Python, on the client side you have Skulpt, a limited implementation of Python that lacks some magic methods, introspection, standard libraries, and other things rarely used that would needlessly bloat the Javascript library.

If you really need something, ask here in the forum. The Anvil crew are maintainers of Skulpt and they may add what you ask for. I have seen it happening a few times.

In my opinion this specific feature, the __code__ magic method, is very rarely used and will only be implemented if it’s very easy.

3 Likes

It might also help if you give some context as to why you need to work out the argument count. There will no doubt be alternatives that work on the Anvil client, once people understand the overall problem you’re trying to solve.

2 Likes

Ah, I was unaware of the client side using a different implementation. Thank you for your explanation!

Seems like a fairly essential feature to me, but well, I am clearly biased lol.

I already have a work-around figured out, my intention was to point out what I thought was a technical issue rather than ask for an alternative, but I appreciate the helpful intent :slight_smile:

Browsers only speak Javascript.
Anvil uses Skulpt to translate Python to Javascript under the hood for you.
It also takes care of managing function calls and getting the returned values when calls travel across the server/client or server/uplink border.

You are working with several Python interpreter running on different computers, seamlessly talking to each other!
Pretty cool, huh?


I guess I’m biased on the other side :slight_smile:

I’ve been speaking python for a few years now, and I didn’t even know that __code__ existed. I looked it up, and I found very little documentation about it. The official Python documentation mentions it only a handful of times. I looked it up more, I found out what it does and now I’m sure I will never use it :slight_smile:

>>> from types import CodeType
>>> CodeType.__doc__.split("\n")[-1]
'Create a code object.  Not for the faint of heart.'
1 Like

That is indeed pretty cool. I was so preoccupied with building what I needed to build that I never really stopped to think about how Anvil does things lol, really interesting tool. Thanks again, I really appreciate your insight :slight_smile:

Haha, well there we have it! Thank you for looking into this :slight_smile:

Fun fact for about Anvil for a new user like you: if you want to render @stucork useless, kind of a DoS attack, all you need to do is ask questions like this, and all of a sudden, squirrel!, he will accept the challenge and, squirrel!, will not be happy until he found a solution!

That’s why he’s contributing to Skulpt and Anvil Extras and Anvil itself.
Poor Stu!

5 Likes

bold of you to assume this will render him useless as opposed to Simply More Powerful :stuck_out_tongue:

6 Likes

Well now I feel bad for calling squirrel :joy:

1 Like