[Plugin] Python
#16

Quote:
Originally Posted by Retardedwolf
Посмотреть сообщение
You should list the advantages comparing Python to Pawn.
The number of built-in data structures Python has is a big advantage. Python not only offers more of them, but they're more lightweight to use in code since you don't have to pre-declare or initialize variables. I did a bunch of code conversion for another Python plugin, and the number of types available to you just make implementing common SA-MP stuff more elegant.

From adminspec.pwn:

Код:
new gSpectateID[MAX_PLAYERS];
new gSpectateType[MAX_PLAYERS];

*snip*

public OnPlayerInteriorChange(playerid, newinteriorid, oldinteriorid)
{
	// IF ANYONE IS SPECTATING THIS PLAYER, WE'LL ALSO HAVE
	// TO CHANGE THEIR INTERIOR ID TO MATCH
	new x = 0;
	while(x!=MAX_PLAYERS) {
	    if( IsPlayerConnected(x) &&	GetPlayerState(x) == PLAYER_STATE_SPECTATING &&
			gSpectateID[x] == playerid && gSpectateType[x] == ADMIN_SPEC_TYPE_PLAYER )
   		{
   		    SetPlayerInterior(x,newinteriorid);
		}
		x++;
	}
}

*snip*

                // end assignments from /specplayer

		gSpectateID[playerid] = specplayerid;
		gSpectateType[playerid] = ADMIN_SPEC_TYPE_PLAYER;

*snip*

                // bit from /specoff where both arrays are set to null values
		gSpectateID[playerid] = INVALID_PLAYER_ID;
		gSpectateType[playerid] = ADMIN_SPEC_TYPE_NONE;
By comparison here are the equivalent bits from my adminspec.py:

Код:
# the original gSpectateID and gSpectateType arrays have been combined
# into one dictionary of 2-tuples, (id,type)

gSpectate = {}

def OnPlayerInteriorChange(playerid, newinteriorid, oldinteriorid):
    # IF ANYONE IS SPECTATING THIS PLAYER, WE'LL ALSO HAVE
    # TO CHANGE THEIR INTERIOR ID TO MATCH
    for x in range(0,MAX_PLAYERS):
        if IsPlayerConnected(x) and GetPlayerState(x) == PLAYER_STATE_SPECTATING and\
           gSpectate.has_key(x) and gSpectate[x] == (playerid,ADMIN_SPEC_TYPE_PLAYER):
            SetPlayerInterior(x,newinteriorid)

*snip*

        # same bit from /specplayer - now it's just one line
        gSpectate[playerid] = (id,ADMIN_SPEC_TYPE_PLAYER)

*snip*

        # same bit from /specoff - instead of setting items in two arrays to null values
        # we just remove the entry entirely
        gSpectate.pop(playerid)
Instead of using two MAX_PLAYERS length arrays, the Python version combines them into one dictionary that holds another data structure called a tuple. A tuple is pretty much an immutable array. The nice thing about tuples is that you can create them on the fly and try to evaluate them in-line. So instead of

Код:
	    if( IsPlayerConnected(x) &&	GetPlayerState(x) == PLAYER_STATE_SPECTATING &&
			gSpectateID[x] == playerid && gSpectateType[x] == ADMIN_SPEC_TYPE_PLAYER )
we can use

Код:
        if IsPlayerConnected(x) and GetPlayerState(x) == PLAYER_STATE_SPECTATING and\
           gSpectate.has_key(x) and gSpectate[x] == (playerid,ADMIN_SPEC_TYPE_PLAYER):
            SetPlayerInterior(x,newinteriorid)
The underlined part in the Python version is the in-line tuple comparison. (playerid,ADMIN_SPEC_TYPE_PLAYER) is a tuple.

I don't know if that does a good job selling just how much simpler and cleaner Python scripts are, especially compared to Pawn, but once you're familiar with the language and know how to use the built-ins you're provided with, you can do more in Python with less time and less code than you can in Pawn.
Reply


Messages In This Thread
Python - by Fabsch - 25.12.2010, 19:37
Re: Python - by [L3th4l] - 25.12.2010, 19:57
Re: Python - by HyperZ - 25.12.2010, 20:24
Re: Python - by Guest3598475934857938411 - 25.12.2010, 20:31
Respuesta: Python - by anonymousx - 25.12.2010, 21:56
Re: Python - by Retardedwolf - 25.12.2010, 22:02
Re: Python - by Mean - 25.12.2010, 22:09
Re: Python - by mkr - 26.12.2010, 08:47
Re: Python - by Wyu - 26.12.2010, 09:00
AW: Python - by Fabsch - 27.12.2010, 18:39
Re: Python - by yass0016 - 29.12.2010, 23:43
Re: Python - by _rAped - 30.12.2010, 00:11
Re: Python - by Lorenc_ - 30.12.2010, 00:28
Re: Python - by TheRob - 30.12.2010, 01:00
Re: Python - by Retardedwolf - 30.12.2010, 01:12
Re: Python - by mkr - 30.12.2010, 08:20
AW: Re: Python - by Fabsch - 30.12.2010, 14:08
Re: Python - by Retardedwolf - 30.12.2010, 19:22
Re: Python - by mkr - 05.01.2011, 22:18
AW: Python - by Fabsch - 06.01.2011, 13:35
Re: Python - by Leeroy. - 06.01.2011, 13:49
Re: Python - by GaGlets(R) - 06.01.2011, 14:47
AW: Python - by Fabsch - 06.01.2011, 15:55
Re: Python - by Wyu - 06.01.2011, 16:37
Re: Python - by ToPhrESH - 09.01.2011, 03:21
Re: Python - by TheYoungCapone - 09.01.2011, 03:50
AW: Re: Python - by Fabsch - 09.01.2011, 18:41
Re: Python - by mkr - 21.05.2011, 02:35
AW: Re: Python - by Fabsch - 21.05.2011, 16:11
Re: Python - by Sfinx_17 - 24.07.2011, 23:01
Re: Python - by GangsTa_ - 25.07.2011, 07:29
AW: Re: Python - by Fabsch - 25.07.2011, 10:38
Re: Python - by Sfinx_17 - 26.07.2011, 18:23
AW: Python - by Fabsch - 28.07.2011, 13:12
Re: Python - by Trenico - 19.01.2012, 22:12
AW: Python - by Fabsch - 20.01.2012, 11:17
Re: Python - by gephaest - 20.01.2012, 22:34
Re: Python - by Deskoft - 20.01.2012, 23:17
AW: Python - by NaS - 20.01.2012, 23:57
Re: Python - by gephaest - 21.01.2012, 08:30
AW: Python - by NaS - 22.01.2012, 00:02
AW: Python - by Fabsch - 22.01.2012, 11:31

Forum Jump:


Users browsing this thread: 1 Guest(s)