Skip to content

Instantly share code, notes, and snippets.

@cocoatomo
Created May 14, 2011 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cocoatomo/972194 to your computer and use it in GitHub Desktop.
Save cocoatomo/972194 to your computer and use it in GitHub Desktop.
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