The OpenNET Project
Хинт: Ссылка в каталоге ПО не работает ? Пожалуйста, сообщите на mc@tyumen.ru и попытайтесь скачать искомую программу с ftp://ftp.opennet.ru.
 
bsd, linux, cisco, web, palm, unix, ...
Поиск (ключи):  ПРОГРАММЫ СТАТЬИ СОВЕТЫ ФОРУМ
ССЫЛКИ НОВОСТИ (+) MAN'ы ДОКУМЕНТАЦИЯ
OpenSource.com.ua – Диски с Linux, FreeBSD, документация, атрибутика. CD от 5 грн, DVD от 17.95 грн!
ASPLinux 9.2 3CD - 15 грн.
SUSE 9.2 DVD - 24.95 грн.
Debian Sarge 13 CD - 52 грн.
Fedora Core 3 4CD - 20 грн.
Linux XP Pro 4CD - 20 грн.
Mandrake 10.1 4CD - 25 грн.
FreeBSD 5.3 3CD - 15 грн.
Кружка "Linux" белая - 19.95 грн.

Поиск:  Каталог документации / Русская документация / Программирование, языки / Python

3.8. Class attributes

You already know about data attributes, which are variables owned by a specific instance of a class. Python also supports class attributes, which are variables owned by the class itself.

Пример 3.18. Introducing class attributes


class MP3FileInfo(FileInfo):
    "хранит ID3v1.0 MP3 теги"
    tagDataMap = {"title"   : (  3,  33, stripnulls),
                  "artist"  : ( 33,  63, stripnulls),
                  "album"   : ( 63,  93, stripnulls),
                  "year"    : ( 93,  97, stripnulls),
                  "comment" : ( 97, 126, stripnulls),
                  "genre"   : (127, 128, ord)}
>>> import fileinfo
>>> fileinfo.MP3FileInfo            1
<class fileinfo.MP3FileInfo at 01257FDC>
>>> fileinfo.MP3FileInfo.tagDataMap 2
{'title': (3, 33, <function stripnulls at 0260C8D4>), 
'genre': (127, 128, <built-in function ord>), 
'artist': (33, 63, <function stripnulls at 0260C8D4>), 
'year': (93, 97, <function stripnulls at 0260C8D4>), 
'comment': (97, 126, <function stripnulls at 0260C8D4>), 
'album': (63, 93, <function stripnulls at 0260C8D4>)}
>>> m = fileinfo.MP3FileInfo()      3
>>> m.tagDataMap
{'title': (3, 33, <function stripnulls at 0260C8D4>), 
'genre': (127, 128, <built-in function ord>), 
'artist': (33, 63, <function stripnulls at 0260C8D4>), 
'year': (93, 97, <function stripnulls at 0260C8D4>), 
'comment': (97, 126, <function stripnulls at 0260C8D4>), 
'album': (63, 93, <function stripnulls at 0260C8D4>)}
1 MP3FileInfo is the class itself, not any particular instance of the class.
2 tagDataMap is a class attribute: literally, an attribute of the class. It is available before creating any instances of the class.
3 Class attributes are available both through direct reference to the class and through any instance of the class.
Замечание
In Java, both static variables (called class attributes in Python) and instance variables (called data attributes in Python) are defined immediately after the class definition (one with the static keyword, one without). In Python, only class attributes can be defined here; data attributes are defined in the __init__ method.

Class attributes can be used as class-level constants (which is how we use them in MP3FileInfo), but they are not really constants.[4] You can also change them.

Пример 3.19. Modifying class attributes

>>> class counter:
...     count = 0                     1
...     def __init__(self)
...         self.__class__.count += 1 2
...     
>>> counter
<class __main__.counter at 010EAECC>
>>> counter.count                     3
0
>>> c = counter()
>>> c.count                           4
1
>>> counter.count
1
>>> d = counter()                     5
>>> d.count
2
>>> c.count
2
>>> counter.count
2
1 count is a class attribute of the counter class.
2 __class__ is a built-in attribute of every class instance (of every class). It is a reference to the class that self is an instance of (in this case, the counter class).
3 Because count is a class attribute, it is available through direct reference to the class, before we have created any instances of the class.
4 Creating an instance of the class calls the __init__ method, which increments the class attribute count by 1. This affects the class itself, not just the newly created instance.
5 Creating a second instance will increment the class attribute count again. Notice how the class attribute is shared by the class and all instances of the class.

Footnotes

[4] There are no constants in Python. Everything can be changed if you try hard enough. This fits with one of the core principles of Python: bad behavior should be discouraged but not banned. If you really want to change the value of None, you can do it, but don't come running to me when your code is impossible to debug.


Закажи себе дистрибутив! Linux и BSD почтой от ЛинуксЦентра:
 1. Mandrakelinux 10.1 Official LC Edition - download version (1DVD) - 250 руб.
 2. Java для Linux (1CD) - 80 руб.
 3. Knoppix 3.7 Russian Edition (1CD) - 90 руб.
 4. Журнал "Системный администратор", 2004 год, декабрь. - 145 руб.
 5. LinuxCenter Games Collection vol.2 (1DVD) - 295 руб.
 6. ASPLinux v10 Express (3CD) - 300 руб.
 7. GoboLinux v. 011 (1CD) - 95 руб.
 8. TheOpenCD v2.0 - лучшее свободное ПО для Windows (1CD) - 95 руб.
 9. Debian GNU/Linux Sarge, октябрь 2004 (2DVD) - 500 руб.
 10. FreeBSD PORTS Winter 2004 Update DVD (1 DVD) - 250 руб.

Закладки на сайте
Проследить за страницей
Created 1996-2005 by Maxim Chirkov  
ДобавитьРекламаВебмастеруЦУПГИД  
RUNNet TopList