How to Call a Static Method on a Variable Class

How to call static method from a static variable inside a class?

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.

How can static method access class variable in Python?

You can access it as InviteManager.INVITE_MESSAGE, but a cleaner solution is to change the static method to a class method:

@classmethod
@missing_input_not_allowed
def invite(cls, email):
return cls.INVITE_MESSAGE

(Or, if your code is really as simple as it looks, you can replace the whole class with a bunch of functions and constants in a module. Modules are namespaces.)

PHP variable class static method call

The problem is that you are access are property from a class in the first useage, but then in the second try you are parsing the value of the class property (into $c), what is a classname as string, and this can used for static calls to static class functions. The first try, trys to access the static method on an string (the class property).

class a {
static function b(){echo'works';}
}
$a='a';
$a::b();

But the real issue of the error is, that this ->FooBar:: is an syntax error in PHP.

calling static method with static class variable as class name in php

You could do that:

$tmp = self::$className;
$tmp::Bar();

Edit

Based on your comments it seems your problem is more about OOP design than it is about syntax. Furthermore, you keep adding new restrictions every time a solution is given, which makes it difficult to provide a relevant answer.

Anyway, I'll try to summarize your options. The syntax you want does not exist (at the moment, anyway), so you have to work around it one way or another. Yes, this is annoying, and yes this means that you will have to make concessions. But that's how it is.

Here are your options so far:

  • Use call_user_func or forward_static_call or similar.
  • Use a temporary local variable. Possibly wrap that into a method if it's really bothering you (e.g. static function call($method) { $tmp = self::$classname; return $tmp::$method(); } and then use self::call('bar');)
  • Refactor your object design using instances instead of static methods so that you don't need to do that anymore.
  • Use some other terribly ugly and dangerous hack (e.g. eval(self::$classname.'::bar();'); and hope it won't come bite you in the butt.

Is it possible to call a static method of another class from a non static method without instance in java?

firstly modifier static not allowed at class pgm.

If you want to call func and x in class legs.

You must use public final then your class name and declare all member of class as static.

After then you need to get reference for class pgm.

So your code will be

  import java.io.*;
import java.util.*;
public final class pgm
{
static int x,v;
static void func()
{
System.out.println("Function run");
}
}

class egs
{
public static void main(String args[])
{
pgm p=null; //ref here
p.func(); // use ppm func here
try
{
p.x=10;
p.func();
}
catch(NullPointerException e)
{
System.out.println("Null caught");
}

}
}

You would get what you want.

Never get confused static used for compile whole block, method, variable at compile time so you can call anything which is static at run time without any instantiation (using new).

Right way I provide you above.

Class (static) variables and methods

Variables declared inside the class definition, but not inside a method are class or static variables:

>>> class MyClass:
... i = 3
...
>>> MyClass.i
3

As @millerdev points out, this creates a class-level i variable, but this is distinct from any instance-level i variable, so you could have

>>> m = MyClass()
>>> m.i = 4
>>> MyClass.i, m.i
>>> (3, 4)

This is different from C++ and Java, but not so different from C#, where a static member can't be accessed using a reference to an instance.

See what the Python tutorial has to say on the subject of classes and class objects.

@Steve Johnson has already answered regarding static methods, also documented under "Built-in Functions" in the Python Library Reference.

class C:
@staticmethod
def f(arg1, arg2, ...): ...

@beidy recommends classmethods over staticmethod, as the method then receives the class type as the first argument.



Related Topics



Leave a reply



Submit