php支持泛型么,在Python中,如何像PHP的“self”关键字那样以静态方式泛型引用类?...

PHP类可以在静态上下文中使用关键字“self”,如下所示:<?php

class Test {

public static $myvar = 'a';

public static function t() {

echo self::$myvar; // Generically reference the current class.

echo Test::$myvar; // Same thing, but not generic.

}

}

?>

显然,我不能在Python中以这种方式使用“self”,因为“self”不是指类而是指实例。那么,有没有一种方法可以在Python的静态上下文中引用当前类,类似于PHP的“self”?

我想我要做的是不太像Python。不过不确定,我对Python还不熟悉。下面是我的代码(使用Django框架):class Friendship(models.Model):

def addfriend(self, friend):

"""does some stuff"""

@staticmethod # declared "staticmethod", not "classmethod"

def user_addfriend(user, friend): # static version of above method

userf = Friendship(user=user) # creating instance of the current class

userf.addfriend(friend) # calls above method

# later ....

Friendship.user_addfriend(u, f) # works

我的代码按预期工作。我只想知道:在静态方法的第一行,有没有一个关键字可以代替“友谊”?

这样,如果类名更改,静态方法就不必编辑。如果类名改变,静态方法就必须被编辑。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值