Nesneler PHP ve Python statik yöntemler, iki farklı dünyalar ...?

0 Cevap php

Ben python dünyasına girmeye çalışırken, php kodlayıcı değilim, ve benim için çok zor.

En php statik yöntemler zevk örneğinin otomatik kurucusudur. Bir kez bunu gerekirse (tek satır, ya da farklı yapıcı params) ile her dosya, nesneyi bildirmek gerek yok

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

echo Foo::aStaticMethod();
?>

biz yok statik yöntem kurucu arayabilirim? ve bunu basit bir yöntem olurdu sınıfta herşeye ulaşabilirler ... biz bile php sınıfta statik oluşturucu var ve bu yüzden böyle arayabilirsiniz: Nesne :: construct () -> myMethod (); (Farklı params her zaman geçirmek için)

ama Python?? @ Durukyöntem sınıfta yöntem tamamen hiçbir şey görmez, basit bir işlev yapar?

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()

Ben tanrının lanet statik yöntem şey erişemez, o tek başına bir işlevi veya bunun gibi bir şey gibi ..?

Belki yanlış dekoratör kullanıyorum? Belki Python statik yöntemlerle php teklifi gibi bir şey var?

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 tell 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

Teşekkür ederim.

0 Cevap