开发者

PHP and Python static methods in objects, two different worlds...?

开发者 https://www.devze.com 2023-01-27 20:17 出处:网络
i\'m php coder, trying to get into python world, and it\'s very hard for me. Biggest enjoy of static methods in php is automatic builder of instance. No need to declare object, if you needed it once,

i'm php coder, trying to get into python world, and it's very hard for me.

Biggest enjoy of static methods in php is automatic builder of instance. No need to declare object, if you needed it once, in every file (or with different constructor params , in one line)

<?php
class Foo { 
    function __constructor__(){
        $this->var = 'blah';
    }
   public static function aStaticMethod() {
       return $this->var;
   }
}

echo Foo::aStaticMethod();
?>

we can call constructor from static method don't we? and we can access everything in class as it would be simple method ... we can even have STATIC CONSTRUCTOR in php class and call it like so: Object::construct()->myMethod(); (to pass different params every time)

but not in python???? @staticmethod makes method in class a simple function that doesn't see totally anything ??

class socket(object):

def __init__(self):
    self.oclass = otherclass()
    print 'test' # does this constructor called at all when calling static method??                

@staticmethod
def ping():
    return self.oclass.send('PING') # i can't access anything!!!


print Anidb.ping()

I can't access anything from that god damned static method, it's like a standalone function or something like this..??

Maybe I'm using the wrong decorator? Maybe there's something like php offers with static methods in python?

1) Please tell why static methods is isolated 2) Please tell me how to make the same behavior like php static methods have. 3) Please 开发者_如何学Pythontell me alternative practical use of this, if php static methods behavior is a bad thing

P.s. the goal of all this to write totally less code as much as possible. P.p.s Heavy commenting of sample code is appreciated

Thank you.


static methods in PHP are not as you believe, they can't access to instance members. No $this! with them.

<?php
class Foo {

    public static $var = 'foo ';

    function __construct(){
        echo 'constructing ';
        $this->var = 'blah ';
    }

    public function aMethod() {
        return $this->var;
    }
    public static function aStaticMethod() {
        #return $this->$var;  -> you can't do that,
        # $this can be accessed only in instance methods, not static
        return self::$var;
    }
}

$foo = new Foo();
echo $foo->aMethod();

echo Foo::aStaticMethod();

?>

Python has three kind of methods in objects static methods are like functions defined ouside classes, the only use to put them in object is to keep them with the class as helper functions. class methods can access only to variables defined in the class (decorator @classmethod). This is more or less what PHP calls static members or methods. The first parameter of such methods sould be cls, and content of class can be accessed through cls. Normal methods must get self as first parameter and are the only ones to be able to access to instance members.

If you want several objects of the same type you definitely need instances, and the other types are not what you are looking for. If you only have one instance of an object, you could use class methods instead (or PHP static methods).

But in most case you should not bother doing that if you don't know why and just stick with instances of objects and normal methods, doing otherwise is premature optimization and your code is likely to bite you later because of the many restrictions you introduce.


You want classmethod instead. That provides the class as the first argument.

EDIT:

class C(object):
  foo = 42

  @classmethod
  def printfoo(cls):
    print cls.foo

C.printfoo()


I see you've already accepted another answer, but I'm not sure that it will work with your code. Specifically, the oclass variable is only created for instances of the class, not for the class itself. You could do it like this:

class socket(object):
    oclass = otherclass()                

    @classmethod
    def ping(cls):
        return cls.oclass.send('PING')

socket.ping()

However, using your existing code and removing all decorators, you could simply instantiate it and use a method on the same line:

socket().ping()
0

精彩评论

暂无评论...
验证码 换一张
取 消