рдЖрдЬ рдореИрдВ рдЗрд╕ рдкреНрд░рд╢реНрди рдХрд╛ рдЙрддреНрддрд░ рджреЗрдиреЗ рдХреА рдХреЛрд╢рд┐рд╢ рдХрд░реВрдВрдЧрд╛: "рдХреНрдпрд╛ рд╣реЛрдЧрд╛ рдЕрдЧрд░ PHP рдХреЗ рдмрдЬрд╛рдп Python рдореЗрдВ рд▓рд┐рдЦреЗрдВ?"
рдореИрдВрдиреЗ рд╕реНрд╡рдпрдВ рдЗрд╕ рдкреНрд░рд╢реНрди рдкрд░ рд▓рдВрдмреЗ рд╕рдордп рддрдХ рдЖрд╢реНрдЪрд░реНрдп рдХрд┐рдпрд╛ рд╣реИред рдореИрдВ 11 рд╕рд╛рд▓ рд╕реЗ PHP рдореЗрдВ рд▓рд┐рдЦ рд░рд╣рд╛ рд╣реВрдВ рдФрд░ рдпрд╣рд╛рдВ рддрдХ тАЛтАЛрдХрд┐ рдПрдХ рдкреНрд░рдорд╛рдгрд┐рдд рд╡рд┐рд╢реЗрд╖рдЬреНрдЮ рднреА рд╣реВрдВред рдореИрдВрдиреЗ рд╕реАрдЦрд╛ рдХрд┐ рдХреИрд╕реЗ рдЗрд╕реЗ "рдкрдХрд╛рдирд╛" рдЪрд╛рд╣рд┐рдП рддрд╛рдХрд┐ рдпрд╣ рдареАрдХ рдЙрд╕реА рддрд░рд╣ рдХрд╛рдо рдХрд░реЗ рдЬреИрд╕рд╛ рдореБрдЭреЗ рдЪрд╛рд╣рд┐рдП рдерд╛ред рдФрд░ рдЬрдм рдореИрдВрдиреЗ рдПрдХ рдмрд╛рд░ рдлрд┐рд░ рд╕реЗ Habr├й рдкрд░ рдПрдХ рд▓реЗрдЦ рдХрд╛ рдЕрдиреБрд╡рд╛рдж рдкрдврд╝рд╛ рдХрд┐ рдХреИрд╕реЗ PHP рдореЗрдВ рд╕рдм рдХреБрдЫ рдЦрд░рд╛рдм рд╣реИ , рддреЛ рдореИрдВ рдмрд╕ рд╣реИрд░рд╛рди рдерд╛ред рд╣рд╛рд▓рд╛рдВрдХрд┐, рдорд╛рдорд▓рд╛ рд░реВрдмреА рдФрд░ рдлрд┐рд░ рдкрд╛рдпрдерди рдореЗрдВ рд╕реНрдерд╛рдирд╛рдВрддрд░рд┐рдд рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рдмрджрд▓ рдЧрдпрд╛ред рдореИрдВ рдЕрдВрддрд┐рдо рдПрдХ рдкрд░ рд░реБрдХ рдЧрдпрд╛, рдФрд░ рдЕрдм рдореИрдВ рдЖрдкрдХреЛ PHP-shnik рдмрддрд╛рдиреЗ рдХреА рдХреЛрд╢рд┐рд╢ рдХрд░реВрдВрдЧрд╛ рдХрд┐ рд╣рдо рдХреИрд╕реЗ рдЕрдЬрдЧрд░ рдХреА рддрд░рд╣ рдорд╣рд╕реВрд╕ рдХрд░рддреЗ рд╣реИрдВред

рдЕрдиреБрдЪреНрдЫреЗрдж рдкреНрд░рд╛рд░реВрдк
рдирдИ рднрд╛рд╖рд╛ рд╕реАрдЦрдиреЗ рдХрд╛ рд╕рдмрд╕реЗ рдЕрдЪреНрдЫрд╛ рддрд░реАрдХрд╛ рдирд┐рдпрдорд┐рдд рд░реВрдк рд╕реЗ рдЙрдкрдпреЛрдЧ рдХрд┐рдП рдЬрд╛рдиреЗ рд╡рд╛рд▓реЗ рдХреА рддреБрд▓рдирд╛ рдХрд░рдирд╛ рд╣реИ рдпрджрд┐ рдирдИ рднрд╛рд╖рд╛ рдореМрдЬреВрджрд╛ рдПрдХ рд╕реЗ рдЕрд▓рдЧ рдирд╣реАрдВ рд╣реИред рд░реВрдмреА рд╕рд╛рдЗрдЯ рдкрд░ рдПрдХ рдЕрдЪреНрдЫрд╛ рдкреНрд░рдпрд╛рд╕ рдХрд┐рдпрд╛ рдЧрдпрд╛ рдерд╛, рд▓реЗрдХрд┐рди рджреБрд░реНрднрд╛рдЧреНрдп рд╕реЗ рдХреБрдЫ рдЙрджрд╛рд╣рд░рдг рд╣реИрдВред
рдореБрдЭреЗ рдпрд╣ рднреА рдзреНрдпрд╛рди рджреЗрдирд╛ рдЪрд╛рд╣рд┐рдП рдХрд┐ рдореИрдВ рдЧрддрд┐рд╡рд┐рдзрд┐ рдХреЗ рд╕рднреА рдкрд╣рд▓реБрдУрдВ рдХреА рддреБрд▓рдирд╛ рдирд╣реАрдВ рдХрд░реВрдВрдЧрд╛, рд▓реЗрдХрд┐рди рдХреЗрд╡рд▓ рд╡реЗ рдЬреЛ рдирдИ рднрд╛рд╖рд╛ рдХреЗ рд╕рд╛рде рдХрд╛рдо рдХрд░рдиреЗ рдХреЗ рдкрд╣рд▓реЗ рд╣рдлреНрддреЛрдВ рдореЗрдВ рдЖрдВрдЦ рдХреЛ рдкрдХрдбрд╝ рд▓реЗрдВрдЧреЗред
рдХрдВрд╕реЛрд▓ рдХреА рддреИрдпрд╛рд░реА
рдореИрдВрдиреЗ рдЗрд╕ рд▓реЗрдЦ рдХреЛ рдЗрдВрдЯрд░реИрдХреНрдЯрд┐рд╡ рдмрдирд╛рдиреЗ рдХреА рдХреЛрд╢рд┐рд╢ рдХреАред рдЗрд╕рд▓рд┐рдП, рдЬрдм рдореИрдВ рдкрдврд╝рддрд╛ рд╣реВрдВ, рддреЛ рдореИрдВ рдЗрд╕реЗ рдХрдВрд╕реЛрд▓ рдореЗрдВ рдЙрджрд╛рд╣рд░рдг рд╕реЗ рднрд░реНрддреА рдХрд░рдиреЗ рдХреА рд╕рд▓рд╛рд╣ рджреЗрддрд╛ рд╣реВрдВред рдЖрдкрдХреЛ рддреБрд░рдВрдд PHP рдХрдВрд╕реЛрд▓ рдпрд╛ рдмреЗрд╣рддрд░ psysh рдХреА рдЖрд╡рд╢реНрдпрдХрддрд╛ рд╣реЛрдЧреА :
php -a
Python. bpython ipython, , . :
python
:
import rlcompleter import readline readline.parse_and_bind("tab: complete") #
~/.pyrc :
~/.bashrc :
, , :
import rlcompleter import readline readline.parse_and_bind("tab: complete")
~/.bashrc :
export PYTHONSTARTUP="${HOME}/.pyrc" export PYTHONIOENCODING="UTF-8"
, , :
source ~/.bashrc
- Python тАФ (. ). Python тАФ , , , , , . : Ruby.
- PHP тАФ . PHP тАФ , , , . , - , PHP . : JavaScript, Lua, Perl.
- .py . <?PHP , .
- , php.ini. , ( PYTHONIOENCODING). , , , , . , , . ( ). php.ini .
- . , , PHP, . .
- $ (, PHP Perl, тАФ ).
- . , , , , . ( Python 3.8 -, . . PEP 572)
- Python .pyc ( Python < 3.3 PYTHONDONTWRITEBYTECODE), - , . , . IDE . PHP APC, .pyc .
- : NULL, TRUE, FALSE тАФ None, True, False .
: , . , :
:foreach($a as $value) { $formatted = $value.'%'; echo $formatted; }
for value in a: formatted = value + '%' print(formatted)
![]()
, ! . , , . - , , . . Style Guide, -.
. . 99% IDE , . . 2 , - .
тАФ . :
print '0.60' * 5; print '5' == 5; $a = array('5'=>true); print $a[5]; $value = 75; print $value.'%'; $a='0'; if($a) print 'non zero length'; //
.
![]()
, , , PHP 7. , .
:
>>> print "25" + 5 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: cannot concatenate 'str' and 'int' objects
, . , PHP, , 1-2 , .
. , int , None , . . , , , , .
try: custom_price = int(request.GET.get('custom_price', 0)) except ValueError: custom_price = 0
, , , . - , , . , , . , , . , - , .
, , : str, int, bool, long. .
:
$tak = ''; echo " $tak {$tak}."; echo " ".$tak."."; echo sprintf(" %s %1$'.9s.", $tak);
:
etot = '' var = '' print(' %s ' % etot) print(etot + ' , ') print(' %s %s' % (etot, var)) print(' %(etot)s %(var)s' % {'etot': etot, 'var': var}) # print(' {} {}'.format(etot, var)) print(' {1} {0}'.format(var, etot)) print(' {etot} {var}'.format(var=var, etot=etot)) print(f' {etot} {var}') # Python 3.6
.
, Python PHP, тАФ . :
vsstrpos($a, 'tr'); trim($a);
a.index('tr') a.strip()
- ?
vssubstr($a, strpos($a, 'name: '));
a[a.index('name: '):]
. Python 2 ( Python 3 тАФ ). u , . ( ) Python .
>>> len(' ') 19 >>> len(u' ') 10
![]()
PHP 6, .
PHP, , MBString function overloading , , . , .
┬л┬╗
:
$a = 'Hello.\n'; $a[strlen($a)-1] != "\n";
- Python. r, PHP.
a = r'Hello.\n' a[-1] != '\n'
. PHP :
var_dump([0=>1, 'key'=>'value']);
![]()
array , PHP array ( ), ( , ). PHP , SPLFixedArray. , , .
Python 3-4 :
- list тАФ .
a = [1, 2, 3] # a[10] = 11 # # > IndexError: list assignment index out of range a.append(11) # del a[0] # a.remove(11) #
- dict тАФ . , ( PHP ).
d = {'a': 1, 'b': 2, 'c': 3} # d[10] = 11 # d[True] = False # (, , , , ) del d[True] #
- tuple тАФ . . .
t = (True, 'OK', 200, ) # t[0] = False # # > TypeError: 'tuple' object does not support item assignment del t[True] # # > TypeError: 'tuple' object doesn't support item deletion t = ([], ) # (, , , , ) t[0].append(1) # > a == ([1], )
- set тАФ . , .
s = set([1,3,4]) s[0] = False # # > TypeError: 'set' object does not support indexing s.add(5) # s.remove(5) # # s | s # s & s # s - s # s ^ s #
PHP тАФ , . Python , Computer Science , , . ┬л , , ┬╗, тАФ . , :
- -: tuple, set, list dict тАФ , , .
- -: list dict.
- -: , -, , , -, .
- -: dict тАФ OrderedDict.
тАФ . .
PHP require_once , PHP- . CMS , , , spl_autoload_register .
тАФ . , . ( 80 ). :
, tools/logic.py
def is_prime(number): max_number = int(sqrt(number)) for multiplier in range(2, max_number + 1): if multiplier > max_number: break if number % multiplier == 0: return False return True
main.py. , .
from tools.logic import is_prime print(is_prime(79))
. : . PHP mysqli_*, pdo_*, memcached_*, , . ?
- -: . , .
- -: . , . , .
- -: .
, , -, , -, , .
? . , , . , . , IDE, , . : , Java C# , .
*args, **kwargs
:
vsfunction makeyogurt($flavour, $type = "acidophilus") { return "Making a bowl of $type $flavour."; }
def makeyogurt(flavour, ftype="acidophilus"): return "Making a bowl of %s %s." % (ftype, flavour, )
. : , . PHP, 5.6, :
function sum(...$numbers) { $acc = 0; foreach ($numbers as $n) { $acc += $n; } return $acc; } echo sum(1, 2, 3, 4); // echo add(...[1, 2, 3, 4]);
Python :
def acc(*args, **kwargs): total = 0 for n in args: total += n return total print(acc(1, 2, 3, 4)) # print(acc(*[1, 2, 3, 4]))
*args тАФ list , **kwargs тАФ dict .
:
class BaseClass: def __init__(self): print("In BaseClass constructor") class SubClass(BaseClass): def __init__(self, value): super(SubClass, self).__init__() # , Python 3.6: super().__init__() self.value = value def __getattr__(self, name): print("Cannot found: %s" % name) c = SubClass(7) print(c.value)
PHP :
- self $this . self (, ). , Python (, , ).
- PHP, . __construct тАФ __init__. __get тАФ __getattr__ ..
- new. тАФ , .
- . super , . parent:: PHP тАФ .
:
- .
- public, protected, private. runtime , , - . Reflection. protected тАФ . / _%ClassName%__%varname%, .
- static, . Python . Singleton , . , , .
- . , , ( bool), , , . . , . , .
, , . , . , - . . , , , :
, , ┬л тАФ , , тАФ ┬╗. , . , , :
PHP , . Python 2 Python 3. , . , Python 3 : , . ┬л ┬╗, , Python 2.
Python 2 2019 .
, , , .
- composer -> pip
- mod_php -> mod_wsgi
- nginx + php-fpm -> nginx + uwsgi + uwsgi_python
- daemon.io -> tornado, twisted
- Zend Framework -> Django
- phalcon -> falcon
?
- , , .
- .
- , .
- ( ).
- , ( , ).
- (- ).
Python
, - .
- 1- : Dive Into Python, 2- 7- . . 10 Project Euler. , . - bash-, ls BusyBox - . , - , , . , , PHP-, .
- 2- : Django . : , , , , , .
- 3- : , , .
!
: (: dginz, defuz, dsx, Stepanow, Studebecker, svartalf).
: (: yktoo). .
2018: Python 2 3.
2019: Python 3.8 .