0

I'm trying to implement a simple class with static vars and static methods:

class X(object):
    @staticmethod
    def do_something(par):
        # do something with parameter par
        #...
        return something

    static_var = X.do_something(5) #<-- that's how I call the function

But I have the error NameError: name 'X' is not defined.

How to do call this static function?

ggorlen
  • 44,755
  • 7
  • 76
  • 106
mimic
  • 4,897
  • 7
  • 54
  • 93
  • Where do you define `par`? – Red Cricket Nov 23 '18 at 22:19
  • What are you trying to do? `par` is a parameter in `do_something` and you're trying to use `par` outside of that definition. – ggorlen Nov 23 '18 at 22:32
  • @RedCricket I don't know where to define it. I hope somebody can help. – mimic Nov 23 '18 at 22:36
  • @ggorlen How to define the ANS properly? I'm trying to call static function from the static variable and definitely I'm not doing properly but I don't know how to do it, that's the question. – mimic Nov 23 '18 at 22:38
  • You are not using `par` in your method so just get rid of it. – Red Cricket Nov 23 '18 at 22:38
  • @RedCricket What do you mean? This static function takes some parameter, how can I get rid of it? – mimic Nov 23 '18 at 22:39
  • just get rid any references to `par` in your code. Get rid of it in the declaration of the `do_something()` and do not using when you call `do_something()`. – Red Cricket Nov 23 '18 at 22:41
  • It's not clear what you mean by "properly", because your objective to this code isn't clear. You can eliminate the parameter and not pass anything to your function, or you can pass in a valid value that exists if you do choose to keep it. It's up to you, but whatever you do, you can't pass non-existing variables to functions. – ggorlen Nov 23 '18 at 22:43
  • @RedCricket the function MUST get parameter because I need to use it inside the function. I want to make it work WITH parameter, how to do that? – mimic Nov 23 '18 at 22:43
  • Please see the updated question. – mimic Nov 23 '18 at 22:44
  • You need to define `par`--this variable does not exist. We're trying to help! – ggorlen Nov 23 '18 at 22:44
  • @ggorlen I really appreciate your help but I don't understand what I should do. Did you see the updated version? Can you give the answer to this question with working code? Thanks! – mimic Nov 23 '18 at 22:47
  • @ggorlen It was just a typo. I already fixed it. – mimic Nov 23 '18 at 22:49
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/184165/discussion-between-ggorlen-and-mimic). – ggorlen Nov 23 '18 at 22:49

1 Answers1

1

It looks like you'd like to initialize the value of a static class variable using a static function from that same class as it's being defined. You can do this using the following syntax taken from this answer but with an added parameter:

class X:
    @staticmethod
    def do_something(par):
        return par

    static_var = do_something.__func__(5)


print(X.static_var)

Output:

5

Try it!

Referencing a static method of the class X directly inside the X definition fails because X doesn't yet exist. However, since you have defined the @staticmethod do_something, you can call its __func__ attribute with the parameter and assign the result to static_var.

Having said that, more information about the underlying design goal you're trying to implement could reveal a better approach.

ggorlen
  • 44,755
  • 7
  • 76
  • 106