在python中调用类中的方法

时间:2017-03-27 16:53:22

标签: python oop

我试图在一个类中调用方法;该电话是下面的最后一行, self.z()

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />

<title> Add Days and Holidays </title>

</head>
<body>

<pre id="fourWeeks"></pre>
Add <input id="nDaysToAdd" value="0"> days<p>
<button onclick="calcBusinessDay()"> Calculate 

</button>
<div id="debug"</div>

<script type="text/javascript">

var today = new Date();  
var holidays = [
   [2017,2,10], 

before holiday
   [2017,7,4], 
];

Date.prototype.addDays = function (days) { return new 

Date(this.getTime() + days*24*60*60*1000); }

Date.prototype.addBusAndHoliDays = function (days) {
  var cDate = this; 
  var holiday = new Date();
  var c='', h='';
  for (var i=1; i<=days ; i++){
    cDate.setDate(cDate.getDate() + 1);
    if (cDate.getDay() == 6 || cDate.getDay() == 0) { 

days++; }
	else {
	  for (j=0; j<holidays.length; j++) {
	    holiday = new 

Date(holidays[j][0],(holidays[j][1]-1),holidays[j][2]);
		c = cDate.toDateString();  h = 

holiday.toDateString();
	    if (c == h) { days++; }
	  }
	}
  } return cDate;
}

  
Date.prototype.DayList = function (daysToShow) {
  var td = this;
  if (daysToShow == undefined) { daysToShow = 31; }

  var str = '';
  for (var i=0; i<daysToShow; i++) {
    newday = new 

Date(td.getFullYear(),td.getMonth(),(td.getDate()+i));
    str += newday.toDateString()+'\t  =>\t'+i+' actual days 

ahead<br>';
  } return str;
}

function calcBusinessDay() {  

functions
  var today = new Date();
  var N = 

parseInt(document.getElementById('nDaysToAdd').valu

e) || 0;
  
  var wd = today.addDays(N);
  document.getElementById('debug').innerHTML 
    = '<p>'+N+' week days from today 

('+today.toDateString() +') will be on: 

'+wd.toDateString();

  var bd = today.addBusAndHoliDays(N);
  document.getElementById('debug').innerHTML += 

'<p>'+N+' business days will be on: '+bd.toDateString();

  str += '<p>'+newDay.DayList(14);
  document.getElementById('debug').innerHTML += 

'<p>'+str;
  

  
}
</script>

</body>
</html>

然而,我收到此错误:

class Wait:
    def __init__(self,a):
            self.a = a
    def countdown(self,a):
       for remaining in range(self.a, 0, -1):
          sys.stdout.write("\r")
          sys.stdout.write("{:2d} seconds remaining.".format(remaining))
          sys.stdout.flush()
          time.sleep(1)
       sys.stdout.write("\rWait Complete!            \n")
    def z(self):
       self.countdown(100)
    self.z()

如何从此课程中的其他方法调用Traceback (most recent call last): File "./countdown.py", line 6, in <module> class Wait: File "./countdown.py", line 18, in Wait self.z() NameError: name 'self' is not defined

1 个答案:

答案 0 :(得分:2)

问题是 self 没有在类体中定义; self 是每个方法的参数,但此时您不在任何方法内。我想你可能会尝试用100秒的倒计时来测试这个,这意味着你需要在主程序中使用底部代码:

class Wait:
    def __init__(self,a):
          self.a = a
    def countdown(self,a):
       for remaining in range(self.a, 0, -1):
          sys.stdout.write("\r")
          sys.stdout.write("{0:2d} seconds remaining.".format(remaining))
          sys.stdout.flush()
          time.sleep(1)
       sys.stdout.write("\rWait Complete!            \n")
    def z(self):
       self.countdown(100)

ticker = Wait(10)
ticker.z()

请注意,您的代码会忽略从 z 发送的 100 值,而是使用创建时设置的计时器值。另请注意,我已更正您的格式化输出语句。

你能从这里拿走吗?