我的Python类中的奇怪行为

时间:2015-07-31 15:00:19

标签: python python-2.7 oop

所以这是非常自我解释的。

class Tray(object):
    '''
    A class used to collect information about each unique tray
    '''
    def __init__(self):
        self.cycle = None
        self.cell = None
        self.bIsTilted = False # Assume trays are not tilted
        self.bSendNoData = False # Assume data is sent, modified later if data not sent
        self.trayData = (self.cycle,
                         self.cell,
                         self.bIsTilted,
                         self.bSendNoData)

    def __repr__(self):
        return str(self.trayData)

    def setCycle(self, cycle):
        self.cycle = cycle

    def setCell(self, cell):
        self.cell = cell

如果我运行以下语句:

currentTray = Tray()
currentTray.setCycle(250)
currentTray.setCell(300)
print(currentTray)
currentTray.setCell(25)
print(currentTray.cell)

我的输出是:

(None, None, False, False)
25

所以,我想我想弄清楚为什么我的self.trayData没有根据self.cycle,self.cell等中的值进行更新。这里有什么问题?

3 个答案:

答案 0 :(得分:3)

当您执行以下操作时 -

<body>

<script>    
function checkRadio() {
var selectedAge="";
var selectedBmi="";
var selectedDiabete="";
var description="";
var len = document.row.length;
var i;

function init(){
for (i = 0; i<len; i++) {
    if (document.row[i].value);
    break;
}

if (selectedAge == "") {
    document.getElementByid("radio_error").innnerHTML = "no option selected";
    return false
}
else {
        document.getElementById("radio_error").innerHTML = "";
        return true;
}

 }
init();

}

 </script>


 <script>
function addNumbers()
    {
var val1 = parseInt(document.querySelector('input[name = "selectedAge"]:checked').value);
var val2 = parseInt(document.querySelector('input[name = "selectedBmi"]:checked').value);
var val3 = parseInt(document.querySelector('input[name = "selectedDiabete"]:checked').value);
var val4 = parseInt(document.querySelector('input[name = "description"]:checked').value);
var ansD = document.getElementById("answer");

ansD.value = val1 + val2 + val3 + val4;
    }

 </script>

 <script>
 var myDiv=document.getElementsByTagName('div');
 var allDiv=myDiv.length;
 for(var i=0;i<allDiv.i++){
 currentDiv=myDiv[i];

 if (ansD.value<=15){
  div[0].style.display = 'block';
  div[1].style.display = 'none';
  div[2].style.display = 'none'
} else if (ansD.value<=25){
  div[0].style.display = 'none';
  div[1].style.display = 'block';
  div[2].style.display = 'none';
} else {ansD.value>25){
  div[0].style.display = 'none';
  div[1].style.display = 'none';
  div[2].style.display = 'block';
}

   </script>


        <table>

                        <tr>
                            <th scope="col"></th>
                            <th scope="col">noRisk</th>
                            <th scope="col">lowRisk</th>
                            <th scope="col">mediumRisk</th>
                            <th scope="col">HighRisk</th>
                        </tr>
                        <tr>
                            <th scope="row"><div class="lefttext">How old are you?</div></th>

                            <td><input type="radio" id="value1" name="selectedAge" onclick="addNumber(val1)" value="0"checked>1-25</td>
                            <td><input type="radio" id="value1" name="selectedAge" onclick="addNumber(val1)" value="5">26-40</td>
                            <td><input type="radio" id="value1" name="selectedAge" onclick="addNumber(val1)" value="8">41-60</td>
                            <td><input type="radio" id="value1" name="selectedAge" onclick="addNumber(val1)" value="10">1-25</td>
                        </tr>

                        <tr>
                            <th scope="row"><div class="lefttext">What is you BMI?</div></th>
                            <td><input type="radio" id="value2" name="selectedBmi" onclick="addNumber(val2)" value="0" checked>0-25</td>
                            <td><input type="radio" id="value2" name="selectedBmi" onclick="addNumber(val2)" value="0">26-30</td>
                            <td><input type="radio" id="value2" name="selectedBmi" onclick="addNumber(val2)" value="9">31-35</td>
                            <td><input type="radio" id="value2" name="selectedBmi" onclick="addNumber(val2)" value="10">35+</td>
                        </tr>
                        <tr>
                            <th scope="row"><div class="lefttext">Does anybody in your family have diabetes?</div></th>
                            <td><input type="radio" id="value3" name="selectedDiabete" onclick="addNumber(val3)" value="0" checked>No</td>
                            <td><input type="radio" id="value3" name="selectedDiabete" onclick="addNumber(val3)" value="7">Grandparent</td>
                            <td><input type="radio" id="value3" name="selectedDiabete" onclick="addNumber(val3)" value="15">Sibling</td>
                            <td><input type="radio" id="value3" name="selectedDiabete" onclick="addNumber(val3)" value="15">Parent</td>
                        </tr>
                        <tr>
                            <th scope="row"><div class="lefttext">How would you describe your diabete</div></th>
                            <td><input type="radio" id="value4" name="description" onclick="addNumber(val4)" value="0" checked >Low sugar</td>
                            <td><input type="radio" id="value4" name="description" onclick="addNumber(val4)" value="0">Normal sugar</td>
                            <td><input type="radio" id="value4" name="description" onclick="addNumber(val4)" value="7">Quite high sugar</td>
                            <td><input type="radio" id="value4" name="description" onclick="addNumber(val4)" value="10">High sugar</td>
                        </tr>
                    </table>
                     <input type="button" name="submit" value="Submit" onclick="javascript:addNumbers()"/>
                     Total  = <input type="text" id="answer" name="answer" value=""/>



                <section>
                     <h2>Your results:</h2>
                    <div id="lowResult">
                            <p> Your results show that you currently have a low risk of
                            developing diabetes. However, it is important that you
                            maintain a healthy lifestyle in terms of diet and exercise.
                            </p>
                    </div>
                    <div id="mediumResult">
                            <p>Your results show that you currently have a medium risk of
                               developing diabetes. For more information on your risk
                               factors, and what to do about them, please visit our diabetes
                               advice website at <a href="http://www.zha.org.zd">http://www.zha.org.zd</a>.
                            <p>
                    </div>
                    <div id="highRisk">
                            <p>Your results show that you currently have a HIGH risk of
                                developing diabetes. [Your main risk factors are your BMI and
                                your diet.] We advise that you contact the Health Authority to
                                discuss your risk factors as soon as you can. Please fill in our
                                contact form and a member of the Health Authority Diabetes
                                Team will be in contact with you.
                    </div>
                </section>
</body>

self.trayData = (self.cycle, self.cell, self.bIsTilted, self.bSendNoData) 的第一个元素没有指向self.trayData(至少不是你想要它的方式),它指向对象self.cycle指向,{{1} 1}}。因此,当您将新值设置为self.cycle时,它不会自动反映在None

让您的具体案例发挥作用的最简单方法是,在self.cycleself.trayData方法中设置trayData

示例 -

setCycle

或者您可以将setCell定义为属性,而不是在def setCycle(self, cycle): self.cycle = cycle self.trayData = (self.cycle, self.cell, self.bIsTilted, self.bSendNoData) 方法中定义它,示例 -

trayData

然后,您可以使用__init__()使用class Tray(object): ''' A class used to collect information about each unique tray ''' @property def trayData(self): return (self.cycle, self.cell, self.bIsTilted, self.bSendNoData) . . . #rest of the class. 类的对象访问trayData。

示例/演示 -

Tray().trayData

答案 1 :(得分:3)

trayData中设置__init__时,每次调用其他函数时都不会更新。将它作为返回值的类的方法更有意义:

def trayData():
    return (self.cycle, self.cell, self.bIsTilted, self.bSendNoData)

答案 2 :(得分:1)

Python变量不会像您尝试的那样更新。你需要做这样的事情:

self.trayData

基本上,如果您希望self.trayData拥有最新数据,则需要在更新其中的值时更新它。

现在,如果您不需要直接访问def __repr__(self): return (self.cycle, self.cell, self.bIsTilted, self.bSendNoData) ,您可以这样做:

Error: Connection lost: The server closed the connection.
at Protocol.end (/home/user/sqlconnector/node_modules/mysql/lib/protocol/Protocol.js:103:13)
at Socket. (/home/user/sqlconnector/node_modules/mysql/lib/Connection.js:102:28)
at Socket.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:920:16
at process._tickCallback (node.js:415:13)