Search code examples
pygamerect

What's the difference between the .width and .w attribute of a Pygame Rect object?


The official pygame documentation states that there are several virtual attributes which can be used to move and align a pygame.Rect instance:

Virtual Attributes of a PyGame Rect Object

Until now I used myRect.w respectively myRect.h to determine the width or height of an pygame.Rect object. But to complete this graphic I came across the .width and .height attributes.

The interesting thing is that both attributes seem to provide us the same date, as you can see in following code listing:

>>> myRect = pygame.Rect((10, 20), (200,100)) #create a new Rect instance
>>> myRect.w
200
>>> myRect.width
200
>>> myRect.size
(200, 100)

What´s now the difference between these two attribute pairs?


Solution

  • There's no difference. You can take a look at the source of the Rect class:

    static PyGetSetDef rect_getsets[] = {
        { "x", (getter)rect_getleft, (setter)rect_setleft, NULL, NULL },
        { "y", (getter)rect_gettop, (setter)rect_settop, NULL, NULL },
        { "w", (getter)rect_getwidth, (setter)rect_setwidth, NULL, NULL },
        { "h", (getter)rect_getheight, (setter)rect_setheight, NULL, NULL },
        { "width", (getter)rect_getwidth, (setter)rect_setwidth, NULL, NULL },
        { "height", (getter)rect_getheight, (setter)rect_setheight, NULL, NULL },
        { "top", (getter)rect_gettop, (setter)rect_settop, NULL, NULL },
        { "left", (getter)rect_getleft, (setter)rect_setleft, NULL, NULL },
        { "bottom", (getter)rect_getbottom, (setter)rect_setbottom, NULL, NULL },
        { "right", (getter)rect_getright, (setter)rect_setright, NULL, NULL },
        { "centerx", (getter)rect_getcenterx, (setter)rect_setcenterx, NULL, NULL },
        { "centery", (getter)rect_getcentery, (setter)rect_setcentery, NULL, NULL },
        { "topleft", (getter)rect_gettopleft, (setter)rect_settopleft, NULL, NULL },
        { "topright", (getter)rect_gettopright, (setter)rect_settopright, NULL,
         NULL },
        { "bottomleft", (getter)rect_getbottomleft, (setter)rect_setbottomleft,
          NULL, NULL },
        { "bottomright", (getter)rect_getbottomright, (setter)rect_setbottomright,
          NULL, NULL },
        { "midtop", (getter)rect_getmidtop, (setter)rect_setmidtop, NULL, NULL },
        { "midleft", (getter)rect_getmidleft, (setter)rect_setmidleft, NULL, NULL },
        { "midbottom", (getter)rect_getmidbottom, (setter)rect_setmidbottom, NULL,
          NULL },
        { "midright", (getter)rect_getmidright, (setter)rect_setmidright, NULL,
          NULL },
        { "size", (getter)rect_getsize, (setter)rect_setsize, NULL, NULL },
        { "center", (getter)rect_getcenter, (setter)rect_setcenter, NULL, NULL },
    
        { "__safe_for_unpickling__", (getter)rect_getsafepickle, NULL, NULL, NULL },
        { NULL, 0, NULL, NULL, NULL }  /* Sentinel */
    };
    

    You can see e.g. that both w and width call rect_getwidth:

    /*width*/
    static PyObject*
    rect_getwidth (PyRectObject *self, void *closure)
    {
        return PyInt_FromLong (self->r.w);
    }
    

    I would still recommend using width/height instead of w/h for readability.