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 828995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:48:47+00:00 2026-05-15T03:48:47+00:00

I am developing a module for Python using a C API. How can I

  • 0

I am developing a module for Python using a C API. How can I create a variable that is seen as global from Python?

For example, if my module is module, I want to create a variable g that does this job:

import module
print module.g

In particular, g is an integer.

Solution from Alex Martelli

PyObject *m = Py_InitModule("mymodule", mymoduleMethods);
PyObject *v = PyLong_FromLong((long) 23);

PyObject_SetAttrString(m, "g", v);
Py_DECREF(v);
  • 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-05-15T03:48:47+00:00Added an answer on May 15, 2026 at 3:48 am

    You can use PyObject_SetAttrString in your module’s initialization routine, with first argument o being (the cast to (PyObject*) of) your module, second argument attr_name being “g”, third argument v being a variable

    PyObject *v = PyLong_FromLong((long) 23);
    

    (or whatever other value of course, 23 is just an example!-).

    Do remember to decref v afterwards.

    There are other ways, but this one is simple and general.

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

Sidebar

Related Questions

I am developing an interface so that Python programs can call into an existing
I'm developing a Python module in C that parses a very efficient protocol which
I've been developing a Python module in C++ using OpenCV 2.3 through 2.4.2, on
I am developing a http module that hooks into the FormsAuthentication Module through the
I am developing a kernel module that uses unlocked_ioctl. I tested it with kernel
In a module I am developing, I am using the following code. function mymodule_page_alter(&$page)
In the Dot Net Nuke module that I'm developing I need to separate the
A Python module I'm developing has a master configuration file in /path/to/module/conf.conf . The
I am developing a Python package using a text editor and IPython. Each time
I'm developing an inherently multithreaded module in Python, and I'd like to find out

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.