A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2001-February/013094.html below:

[Python-Dev] Those import related syntax errors again...

[Python-Dev] Those import related syntax errors again... [Python-Dev] Those import related syntax errors again...Mark Hammond MarkH@ActiveState.com
Thu, 22 Feb 2001 13:02:28 +1100
[Guido]

> I don't understand.  Methods do have a function declaration:
>
>   class C:
>
>       def meth(self):
>           pass
>
> Or am I misunderstanding?

The problem is I have a class object, and the source-code for the method
body as a string, generated at runtime based on runtime info from the
reflection capabilities of the system we are interfacing to.  The simplest
example is for method code of "return None".

I dont know how to get a code object for this snippet so I can use the new
module to get a new method object.  Attempting to compile this string gives
a syntax error.  There was some discussion a few years ago that adding
"function" as a "compile type" may be an option, but I never progressed it.

So my solution is to create a larger string that includes the method
declaration, like:

"""def foo(self):
      return None
"""

exec that, get the function object out of the exec'd namespace and inject it
into the class.

Mark.




RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4