-1

I'm trying to make a robotics kit. Its designed to be simple so I'm using properties so when the users change a parameter the property method sends the serial command which controls motors/ servos/whatever.

This is the code at the moment, directly from a previous question I asked on here.

class Servo(object):
def __init__(self, which_servo, angle = 0):
    self._angle = angle;
    self._servo_no = which_servo

def get_angle(self):
    return self._angle
def set_angle(self, value):
    self._angle = value
    print "replace this print statement with the code to set servo, notice that this method knows the servo number AND the desired value"

def del_angle(self):
    del self._angle
angle = property(get_angle, set_angle, del_angle, "I'm the 'angle' property.

this is then initialized as such:

class robot(object):
def __init___(self):
    self.servos = [Servo(0), Servo(1), Servo(2), Servo(3)]

Now, this works in the respect that it does change the variable through the getter and setter functions, however the prints in the getter and setter never is printed, thus if I replace it with a serial command I assume it won't do anything either, can anyone shed any light on this?

Thanks

Update: Thanks for the help using the servo file this is whats happened, there are three scenarios the first works and by extension I would have assumed the next two preferable scenarios would work but they don't any ideas?

This works

import servo

class Robot(object):
    def __init__(self):
        self.servos = [servo.Servo(0, 0), servo.Servo(1,0), servo.Servo(2,0)]

R = Robot()

R.servos[1].angle = 25

This does not:

import servo

class Robot(object):
    def __init__(self):
        self.servos = [servo.Servo(0, 0), servo.Servo(1,0), servo.Servo(2,0)]

R = Robot()

left_servo = R.servos[1].angle

left_servo = 25

Neither does this

import servo

class Robot(object):
    def __init__(self):
    self.servos = [servo.Servo(0, 0).angle, servo.Servo(1,0).angle,               servo.Servo(2,0).angle]

R = Robot()

R.servo[1] = 25
dsolimano
  • 8,870
  • 3
  • 48
  • 63
user2137452
  • 143
  • 1
  • 4
  • 7
  • 1
    Does python have private classes? ie: A class (type) defined within another class that no outside class can know exists? I'm guessing you mean a class with private variables here? Note: Did you check to see if your program can output any print statement successfully? (perhaps stdout got redirected?) –  Jul 03 '13 at 16:05

1 Answers1

0

Using the preferred decorator syntax for properties, this works fine. It'll also help you avoid issues like this in the future

class Servo(object):
    def __init__(self, which_servo, angle = 0):
        self._angle = angle;
        self._servo_no = which_servo

    @property
    def angle(self):
        return self._angle

    @angle.setter
    def angle(self, value):
        self._angle = value
        print "replace this print statement with the code to set servo"

    @angle.deleter
    def angle(self):
        del self._angle

Seeing as your indentation is off here, I believe this is likely an issue of indentation in your source. This should work as well if you really want to use the old property function:

class Servo(object):
    def __init__(self, which_servo, angle = 0):
        self._angle = angle;
        self._servo_no = which_servo

    def get_angle(self):
        return self._angle

    def set_angle(self, value):
        self._angle = value
        print "replace this print statement with the code to set servo"

    def del_angle(self):
        del self._angle

    angle = property(get_angle, set_angle, del_angle,"I'm the 'angle' property.")

Both of these work successfully for me (inside a file called servo.py)

>>> import servo
>>> s = servo.Servo(1, 2)
>>> s.angle
2
>>> s.angle = 3
replace this print statement with the code to set servo

EDIT

To address your new issues. When you assign R.servos[1].angle to left_servo, its not creating a reference to the servos angle, it's just setting left_servo to whatever the angle is. When you reassign 25 to it, you're not assigning to the angle you're assigning to the left_servo.

On the second one, I'm assuming you meant R.servos and not R.servo which should be raising an AttributeError. But the real problem as I see it, is you should be saying R.servos[1].angle = 25 and you're omitting the .angle.

To (attempt to) put it simply: When you use the = operator, you are changing where a name refers to, not what it refers to.

>>> x = 1
>>> x = 2

the second assignment does not overwrite the 1 in memory with a 2, it just changes where x refers to. So if I did something like

>>> x = 1
>>> y = x
>>> y = 2
>>> print x
1

the output is 1 because your are telling y to refer to the same place that x refers. Changing y to 2 changes where y refers to, it does not change the 1 already in memory.

Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
  • A no, the indentation is find it just didn't copy to stack overflow very well! – user2137452 Jul 03 '13 at 21:02
  • @user2137452, have you tried using either of these exactly as written? they are both working for me as expected (see edit). – Ryan Haining Jul 03 '13 at 21:06
  • also in addition I'm now getting an error saying robot() has no attribute servos when running this code `code` class robot(object): def __init___(self): self.servos = [Servo(0), Servo(1), Servo(2), Servo(3)] self.cheese = 1 r = robot() print r.servos – user2137452 Jul 03 '13 at 21:11
  • @user2137452 again, please try using my first code block exactly as written and tell if it works or not, because it does. – Ryan Haining Jul 03 '13 at 21:14
  • @user2137452 you're gonna need to post more of your code, something strange may be going on. – Ryan Haining Jul 03 '13 at 21:15
  • Ryan you've been brilliant I've got the code you gave me working but Ive edited above to clarify so extended difficulties, thanks – user2137452 Jul 04 '13 at 10:35
  • @user2137452 ok I'll elaborate on your problems given a little time (really it's own question) but the short answer is you can't do this. You need to understand mutability and how typing works in order to get why. – Ryan Haining Jul 04 '13 at 16:40
  • @user2137452 if you happen to c or c++ I can explain whats going on in terms of that. – Ryan Haining Jul 04 '13 at 19:18