在python的类中管理属性的麻烦

时间:2016-05-29 10:09:11

标签: python

我是初学者,这是我第一次处理OOP。你能帮我弄清楚我做错了什么吗?

class Example(object):

    def __init__(self, a, b):
        self.a = a
        self.b = b
        self.c = self.a + self.b/2

    def property1(self):
        self.obs1 = self.a - self.c

    def property2(self):
        self.obs2 = self.b - self.c


    def get_obs1(self):
        return self.obs1
    def get_obs2(self):
        return self.obs2        

test = Example(2,5)

test.get_obs1()

我获得以下例外情况:

Traceback (most recent call last):
  File "/home/panadestein/Programming_Games/Python/buggy.py", line 22, in <module>
    test.get_obs1()
  File "/home/panadestein/Programming_Games/Python/buggy.py", line 16, in get_obs1
    return self.obs1
AttributeError: 'Example' object has no attribute 'obs1'

1 个答案:

答案 0 :(得分:0)

在调用obs1时,属性property尚未定义,尚未与您的类实例绑定。您的问题已经提示使用__init__,因此请使用此代码并在class Example(object): def __init__(self, a, b): self.a = a self.b = b self.c = self.a + self.b/2 self._obs1 = self.a - self.c self._obs2 = self.b - self.c @property def obs1(self): return self._obs1 @property def obs2(self): return self._obs2 test = Example(2,5) test.obs1 中定义私有变量:

 <?php
                                                #CONNECTION    
    require 'dbcon.php';

                                                #INPUT CONTAINERS
    $ACTIVATION_CODE=$USERNAME=$PASSWORD=$EMAIL=$FIRST_NAME=$MIDDLE_NAME=$LAST_NAME=$CONTACT_NO=$ADDRESS=$PROFILE='';

                                                #ERROR CONTAINERS
    $USERNAME=$PASSWORD=$EMAIL=$FIRST_NAME=$MIDDLE_NAME=$LAST_NAME=$CONTACT_NO=$ADDRESS=$PROFILE=$USER_TYPE='';

                                                #CONNECTION/QUERY#
    $connection = mysqli_connect('localhost','root','','library_system') or die(mysqli_error());    
    $stm = $mysli_link->prepare("INSERT INTO users (username,password,first_name,middle_name,last_name,address,contact_no,email,activation_code) values(?,?,?,?,?,?,?,?,?)");
    $stm = $stm->bind_param($USERNAME,$PASSWORD,$FIRST_NAME,$MIDDLE_NAME,$LAST_NAME,$ADDRESS,$CONTACT_NO,$EMAIL,$ACTIVATION_CODE);
                                                #VERIFICATION SCRIPT
    IF(isset($_POST['register'])){
        $USERNAME = trim(mysqli_real_escape_string($connection,$_POST['username']));
        $PASSWORD = trim(mysqli_real_escape_string($connection,password_hash($_POST['password'],PASSWORD_DEFAULT)));
        $EMAIL = trim(mysqli_real_escape_string($connection,$_POST['email']));
        $FIRST_NAME = mysqli_real_escape_string($connection,$_POST['first_name']);
        $MIDDLE_NAME = mysqli_real_escape_string($connection,$_POST['middle_name']);
        $LAST_NAME = mysqli_real_escape_string($connection,$_POST['last_name']);
        $CONTACT_NO = trim(mysqli_real_escape_string($connection,$_POST['contact_no']));
        $ADDRESS = mysqli_real_escape_string($connection,$_POST['address']);            
        $ACTIVATION_CODE = md5($USERNAME.(rand(0,1000)));
        $USER_TYPE = "ADMIN";
        $stm->execute();                                            

    }

?>

详细了解@property