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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:46:47+00:00 2026-06-13T18:46:47+00:00

I am trying to achieve the following: class A: username = None username =

  • 0

I am trying to achieve the following:

class A:
    username = None
    username = get_username()
    def get_username(self):
        if username is None:
            try:
                uname = os.environ["USER"]
            except:
                printf("Couldn't find a user name")
            return uname
        return username

Not sure how to achieve this. I’m sure I’m missing some “self.” prefixes but this is the first time I’m working with python and static members.

In a sense I want a class with some members and functions to calculate values for these members but I don’t want recalculations. I would also like these to be static functions and data members.

The problem is that the line “username = get_username()” the function hasn’t already been defined. If I put username after the function then it’s not

  • 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-13T18:46:49+00:00Added an answer on June 13, 2026 at 6:46 pm

    First, there’s no reason to assign None to username if you’re just going to reassign it immediately after.

    Second, if you want the method to be a static method, you can’t give it a self argument. And if you want a real static method, you have to declare it explicitly.

        @staticmethod
        def get_username():
            if username is None:
            ...
    

    Otherwise, you need an instance of the class (that self) to call it on, and you don’t have one yet.

    In Python 3, any regular method acts like a static method when called on the class, like an instance method when called on an instance. So, if you’re sure you’re never going to want to call a.get_username() on an instance a, you can skip the decorator. But you still need to get rid of the self parameter.

    I think what you’re actually trying to do is use a class variable to memoize the result of a static method. You can’t do that, but you can use a class variable to memoize the result of a class method, which may be close enough. That would look like this:

    class A:
        username = None
        @classmethod
        def get_username(cls):
            if cls.username is None:
                try:
                    uname = os.environ["USER"]
                except:
                    print("Couldn't find a user name")
                else:
                    cls.username = uname
            return cls.username
    

    On the other hand, there’s no good reason username has to be a class member. You can memoize by adding a member to the function, by passing a mutable default variable, or in various other ways which don’t require infecting the class, and which allow you to leave get_username as a static method instead of a class method.

    But really, the best solution is to find a memoization library on PyPI, in ActiveState’s recipe list, etc., so you can just write this:

    class A:
        @memoize
        @staticmethod
        def get_username():
            try:
                return os.environ["USER"]
            except:
                print("Couldn't find a user name")
                return None
    

    Again, you can drop the @staticmethod if you’re sure nobody’s ever going to try to create an instance of A and call get_username on it.

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

Sidebar

Related Questions

I'm trying to achieve the following: class Data(object): def __init__(self, data): self.data = data
I'm trying to achieve following: class A { def foo() { foo } }
I'm trying to achieve the following with a bash script: try for SSH connection,
I'm trying to achieve the following: Given an abstract class MemoryObject , that every
Suppose I have a base class as following: class User: user_types = [] def
I'm new to Hibernate and I'm trying to achieve the following: the class i'm
I'm trying to achieve the following goal: Using this general singleton class: abstract class
Hi I am trying to achieve the following: 1) User clicks on file input
I'm trying to achieve something like the following in C++: class MyVector; // 3
I am trying to achieve something like the following using OO JavaScript: class Sample

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.