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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:06:50+00:00 2026-06-08T04:06:50+00:00

I am trying to dynamically assign a class variable a value from inside of

  • 0

I am trying to dynamically assign a class variable a value from inside of a class.

class Test:
  dynamic_value = get_dynamic_value()

I believe get_dynamic_value() should belong to the Test class. Is there a way to have Test contain this method?

Right now I am using and it is working

def get_dynamic_value():
  return 'my dynamic value'

class Test:
  dynamic_value = get_dynamic_value()

I would like for Test to contain this method so I have tried to make it both a @classmethod and a @staticmethod and calling it by

class Test:
  dynamic_value = Test.get_dynamic_value()

  @staticmethod
  def get_dynamic_value():
    return 'dynamic'

But when trying it using a static method I receive

AttributeError: class Test has no attribute 'get_dynamic_value'

Is there any way to do this? Or is there a better way to handle this?

  • 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-08T04:06:52+00:00Added an answer on June 8, 2026 at 4:06 am

    Class suites evaluate from top to bottom, and the class you are defining does not exist until after the entire suite is evaluated.

    You can put the function definition inside the class body, but you’ve got to call it after it’s been defined, not before. So, this will work:

    class Test:
    
      def get_dynamic_value():
        return 'dynamic'
    
      dynamic_value = get_dynamic_value()
    

    Marking the function as a staticmethod is not necessary; it’s not used as a method at all, just a function that is called during the evaluation in the class suite. If you don’t want the function to be available after the class is defined (as it isn’t intended as a method), you can delete it.

    class Test:
    
      def get_dynamic_value():
        return 'dynamic'
    
      dynamic_value = get_dynamic_value()
      del get_dynamic_value
    

    If the value is really supposed to be dynamically calculated, every time you ask for it (so it is able to change from access to access), then you should use a property.

    class Test:
    
      @property
      def dynamic_value(self):
        return 'dynamic'
    

    This would only be evaluate when accessed on a class instance, but the property object itself would exist on the class.

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

Sidebar

Related Questions

So I'm trying to figure out if there is some method to dynamically create/assign
I’m trying to dynamically declare an ImageButton. I declare it and assign an ID
I am currently trying to assign an event handler to dynamically created rectangle objects.
I am trying to evaluate two dynamically assigned variables; then attempting to assign a
I'm trying to dynamically create a navbar from data i fetch from a web
I'm trying to assign a class that I have within the jQuery UI accordion
I'm trying to dynamically assign a different instance of jQuery-UI's resizable after the DOM
Jquery/programming newb here. I'm trying to dynamically assign a height to a ul to
I'm trying to ASSIGN dynamically exclusive meta-tags for each page: i.e: url.com/index.php?action=signup Header -
I am trying to dynamically allocate an array of base(Student) classes, and then assign

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.