Created
May 14, 2011 13:03
-
-
Save cocoatomo/972194 to your computer and use it in GitHub Desktop.
PyObject の基礎: ソースコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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