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

  • Home
  • SEARCH
  • 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 6724005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:38:11+00:00 2026-05-26T09:38:11+00:00

I am relatively new to python I would like to run a block of

  • 0

I am relatively new to python
I would like to run a block of code only once for a class. Like the static block in java.

for eg:

class ABC:
    execute this once for a class.

Is there any such options available in python?

In java we write it like this. This is executed only once for a class, at the time the class is loaded. Not for every object creation

public class StaticExample{
    static {
        System.out.println("This is first static block");
    }
}

Thanks

  • 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-26T09:38:12+00:00Added an answer on May 26, 2026 at 9:38 am

    To do this just put the code directly under the class definition (parallel to the function definitions for the class.

    All code directly in the class gets executed upon creation of that type in the class’ namespace. Example:

    class Test:
        i = 3
        y = 3 * i
        def testF(self):
            print Test.y
    
    v = Test()
    v.testF()
    # >> 9
    

    Just to fill out the last bit of information for you: your method function defs are also being executed (just like they get “executed” when you define a function on the global namespace), but they aren’t called. It just happens to be that executing a def has no obviously visible results.

    Python’s object-oriented-ness is quite clever, but it takes a bit to get your head around it! Keep it up, it’s a very fun language.

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

Sidebar

Related Questions

Relatively new to Python.. I would like to know how to check if a
I'm a relatively new convert to Python. I've written some code to grab/graph data
Being relatively new to Python 2, I'm uncertain how best to organise my class
I am relatively new to python (already did some 1h scripts like a little
I'm new to python and I would like some help. I created some classes
I am relatively new to python only about 2 months of learning mostly by
I'm relatively new to the Python world, but this seems very straight forward. Google
I'm relatively new to Python and am having problems programming with Scapy, the Python
I am relatively new to python and app engine, and I just finished my
I'm relatively new to wxPython (but not Python itself), so forgive me if I've

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.