печально быть антисоциальным - Post a comment

Mar. 8th, 2004

[info]smejmoon

04:43 pm - globālie mainīgie un pitons

namespace likumi ir jāapgūst, lai varētu efektīvi rakstīt modulāru kodu.

Pitonā mainīgais viemēr ir lokālajā neimspeisā, ja tas netiek nodefinēts ar atslēgvārdu global.

Seko piemērs:




>>> a = 3
>>> def fu():
... a = 2
...
>>> fu()
>>> a
3
>>> def hu():
... global a
... a = 4
...
>>> a
3
>>> hu()
>>> a
4
>>>
>>> def ah():
... a = a + 1
...
>>> ah()
Traceback (most recent call last):
File "", line 1, in ?
File "", line 2, in ah
UnboundLocalError: local variable 'a' referenced before assignment
>>>


P.S. Kautkā ir sanācis, ka savā kodā es to neizmantoju, bet citiem noder.

Read Comments

Reply:

From:
( )Anonymous- this user has disabled anonymous posting.
Username:
Password:
Subject:
No HTML allowed in subject
  
Message:

Notice! This user has turned on the option that logs your IP address when posting.