開発環境
- OS X Yosemite - Apple (OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python 3.4 (プログラミング言語)
Introducing Python: Modern Computing in Simple Packages(Bill Lubanovic (著)、 O'Reilly Media)のChapter 6(Oh Oh: Objects and Classes)、Things to Do 6.8.を解いてみる。
Things to Do 6.8.
コード(BBEdit)
sample8.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
class Element:
def __init__(self, name, symbol, number):
self.__name = name
self.__symbol = symbol
self.__number = number
@property
def name(self):
return self.__name
@property
def symbol(self):
return self.__symbol
@property
def number(self):
return self.__number
def __str__(self):
return '{0} {1} {2}'.format(self.__name, self.__symbol, self.__number)
d = dict(name='Hydrogen', symbol='H', number=1)
hydrogen = Element(**d)
print(hydrogen.name, hydrogen.symbol, hydrogen.number)
try:
hydrogen.name = 'hydrogen'
except Exception as err:
print(err)
入出力結果(Terminal, IPython)
$ ./sample8.py Hydrogen H 1 can't set attribute $
0 コメント:
コメントを投稿