Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
PyObject の基礎: ソースコード
#include <stdio.h>
struct rect {
int height;
int width;
void (*rotate)(struct rect *);
};
void rect_rotate(struct rect *);
void rect_init(struct rect *self)
{
self->height = 0;
self->width = 0;
self->rotate = *rect_rotate;
}
void rect_rotate(struct rect *self)
{
int tmp = self->height;
self->height = self->width;
self->width = tmp;
}
int main(void)
{
struct rect r;
rect_init(&r);
r.height = 3;
r.width = 4;
printf("height: %d, width: %d\n", r.height, r.width);
r.rotate(&r);
printf("height: %d, width: %d\n", r.height, r.width);
return 0;
}
class Rect(object):
def __init__(self):
self.height = 0
self.width = 0
def rotate(self):
self.height, self.width = self.width, self.height
rect = Rect()
rect.height = 3
rect.width = 4
print('height: {0.height}, width: {0.width}'.format(rect))
rect.rotate()
print('height: {0.height}, width: {0.width}'.format(rect))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment