默认方法参数 [英] Default method arguments

查看:65
本文介绍了默认方法参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

各位大家好!

我没什么问题:


A类:

def __init __(self,n):

self.data = n

def f(self,x = ????)

print x

我想要的是使self.data成为self.f()的默认参数。 (我

想要使用''A''课程如下:


myA = A(5)

myA。 f()


因此得到打印''5'。)

解决方案

2005年11月15日08:03:26 -0800, gr ***** **********@gmail.com

< gr *************** @ gmail.com>写道:

各位大家好!
我有一点问题:

def __init __(self,n):
自我.data = n
def f(self,x = ????)
打印x

我想要的是让self.data成为self.f的默认参数()。 (我想要使用''A''课程如下:

myA = A(5)
myA.f()

结果打印''5'。)




A类:

def __init __(self,n):

self.data = n


def f(self,x = None):

如果不是x:

x = self.data

print x

myA = A(5)
myA.f()






Peace

Bill Mill

bill.mill at gmail.com


>我没有什么问题:


A类:
def __init __(self,n):
self.data = n
def f(self, x = ????)
打印x

我想要的是使self.data成为self.f()的默认参数。 (我想要使用''A''课程如下:

myA = A(5)
myA.f()

结果打印''5'。)




#使用新式课程,如果没有令人信服的理由不这样做

A类(对象):

def __init __(self,n):

self.data = n

def f( self,x =无)

#不要使用if not x !

如果x为无:

print self.data

else:

print x


-

Nicola Larosa - ni ***** **@m-tekNico.net


.... Linux安全性比许多竞争对手好。然而,即使是今天最好的系统也完全不合适。说Linux是比Windows更安全的
还没有真正解决更大的问题

- 两者都不够好。 - Alan Cox,2005年9月


> def f(self,x = None):

if if not x:


哈!你倒下了! ; -D

(提示:如果x传递的值为零?:-))

x = self.data
print x



-

Nicola Larosa - ni *******@m-tekNico.net


.... Linux安全性比许多竞争对手好。然而,即使是今天最好的系统也完全不合适。说Linux是比Windows更安全的
还没有真正解决更大的问题

- 两者都不够好。 - Alan Cox,2005年9月


Hello everybody!
I have little problem:

class A:
def __init__(self, n):
self.data = n
def f(self, x = ????)
print x

All I want is to make self.data the default argument for self.f(). (I
want to use ''A'' class as following :

myA = A(5)
myA.f()

and get printed ''5'' as a result.)

解决方案

On 15 Nov 2005 08:03:26 -0800, gr***************@gmail.com
<gr***************@gmail.com> wrote:

Hello everybody!
I have little problem:

class A:
def __init__(self, n):
self.data = n
def f(self, x = ????)
print x

All I want is to make self.data the default argument for self.f(). (I
want to use ''A'' class as following :

myA = A(5)
myA.f()

and get printed ''5'' as a result.)



class A:
def __init__(self, n):
self.data = n

def f(self, x=None):
if not x:
x = self.data
print x

myA = A(5)
myA.f()


5

Peace
Bill Mill
bill.mill at gmail.com


> I have little problem:


class A:
def __init__(self, n):
self.data = n
def f(self, x = ????)
print x

All I want is to make self.data the default argument for self.f(). (I
want to use ''A'' class as following :

myA = A(5)
myA.f()

and get printed ''5'' as a result.)



# use new-style classes, if there''s no cogent reason to do otherwise
class A(object):
def __init__(self, n):
self.data = n
def f(self, x = None)
# do NOT use "if not x" !
if x is None:
print self.data
else:
print x

--
Nicola Larosa - ni*******@m-tekNico.net

....Linux security has been better than many rivals. However, even
the best systems today are totally inadequate. Saying Linux is
more secure than Windows isn''t really addressing the bigger issue
- neither is good enough. -- Alan Cox, September 2005


> def f(self, x=None):

if not x:
Ha! You fell for it! ;-D
(Hint: what about x being passed with a value of zero? :-) )
x = self.data
print x



--
Nicola Larosa - ni*******@m-tekNico.net

....Linux security has been better than many rivals. However, even
the best systems today are totally inadequate. Saying Linux is
more secure than Windows isn''t really addressing the bigger issue
- neither is good enough. -- Alan Cox, September 2005


这篇关于默认方法参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆