Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8564571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:13:50+00:00 2026-06-11T17:13:50+00:00

In python I can create a class without class statement: MyClass = type(‘X’, (object,),

  • 0

In python I can create a class without class statement:

MyClass = type('X', (object,), dict(a=1))

Is there a way to create a function without ‘def’?
Thats as far as i got…

d={} # func from string
exec'''\
def synthetics(s):
    return s*s+1
''' in d

>>> d.keys()
['__builtins__', 'synthetics']
>>> d['synthetics']
<function synthetics at 0x00D09E70>
>>> foo = d['synthetics']
>>> foo(1)
2
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:13:51+00:00Added an answer on June 11, 2026 at 5:13 pm

    Technically, yes, this is possible. The type of a function is, like all other types, a constructor for instances of that type:

    FunctionType = type(lambda: 0)
    help(FunctionType)
    

    As you can see from the help, you need at minimum code and globals. The former is a compiled bytecode object; the latter is a dictionary.

    To make the code object, you can use the code type’s constructor:

    CodeType = type((lambda: 0).func_code)
    help(CodeType)
    

    The help says this is “not for the faint of heart” and that’s true. You need to pass bytecode and a bunch of other stuff to this constructor. So the easiest way to get a code object is from another function, or using the compile() function. But it is technically possible to generate code objects completely synthetically if you understand Python bytecode well enough. (I have done this, on a very limited basis, to construct signature-preserving wrapper functions for use in decorators.)

    PS — FunctionType and CodeType are also available via the types module.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way I can create a UAC-like environment in Python? I want
I am looking for a way to create a basic python object which I
I want to create a replacement class of the built-in type set that can
I'm using python 3.1.1. I know that I can create byte objects using the
In python we can use multiprocessing modules. If there is a similar library in
In Python I can use get method to get value from an dictionary without
In Python I can define a function as follows: def func(kw1=None,kw2=None,**kwargs): ... In this
I'm trying to create am immutable type (class) in C++, I made it so
While building a new class object in python, I want to be able to
Normally, a base class method in Python can be called from a derived class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.