Who's learning Python?

Chat about anything not covered in these forums, but keep it civil!

Post » Mon Jan 14, 2008 10:14 pm

I've recently started teaching myself Python in anticipation of the upcoming Python support for Construct. I figured if I'm going to be any sort of Construct "power user" I should probably know my way around all the features it has to offer.

So far I'm liking what I've seen of Python. It seems to be a good language for beginning programmers.

Just curious if there's anyone else here who's decided to pick up Python for he same reason...
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

Post » Mon Jan 14, 2008 10:41 pm

I looked into python a long while back, but never bothered to learn it in depth since I didn't think I'd have anything useful to use it for...


Guess I was wrong...
B
4
S
1
G
5
Posts: 65
Reputation: 1,932

Post » Tue Jan 15, 2008 12:53 am

I started a few days ago with some tutorials. Since i've never programmed before, and since i don't know any advanced math i guess i will progress pretty slow. But it was far easier then i expected! I'll definately try to learn more of it.
B
8
S
2
G
5
Posts: 744
Reputation: 3,288

Post » Tue Jan 15, 2008 12:01 pm

I am intensely amused by the stupidest things...

[code:2ozojgyl]
>>> lol = 'butt'
>>> (lol + 's ') * 5
'butts butts butts butts butts '
[/code:2ozojgyl]

I guess it goes back to my earliest programming days:

[code:2ozojgyl]
10 PRINT "YOU STINK!"
20 GOTO 10
[/code:2ozojgyl]
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

Post » Tue Jan 15, 2008 1:51 pm

haha that looks pretty funny

i may look into it on day but i am fine with construct and php for now :)
B
3
S
2
G
5
Posts: 301
Reputation: 2,302

Post » Tue Jan 15, 2008 11:42 pm

Okay, there are a few things that are confusing me about Python. Not that I don't understand them, more like "why the hell did they do it that way?"

For instance:
[code:m6cjgmng]
>>> x = [5, 4, 3, 2, 1]
>>> y = x
>>> y.sort()
>>> x
[1, 2, 3, 4, 5]
[/code:m6cjgmng]

You want y to equal x, and expect it to be it's own variable from there on. but when you sort y (to put it in numerical order) it affects x too because x and y are just two different names for the same friggin variable. They both point to the same list. WTF? To make y separate from x you have to

[code:m6cjgmng]
y = x[:]
[/code:m6cjgmng]

to create a duplicate of the list. So far Python is fairly intuitive, but there are a few little things like this that go against the grain of it.

Another for-instance: problems arising from non-declarative variable types. x can equal 12, or x can equal "dog." Fine. That makes things easy on the face of it. I don't have to declare variable types with $ or % or whatever, like in BASIC. But then you can do weird stuff like this:

[code:m6cjgmng]
>>> spriteParams = {"x": "45", "y": "80", "active": "yes", "jumping": "no"}
>>> if spriteParams["x"] < 50: print spriteParams["x"] + " is less than 50"

(no result)

>>> if spriteParams["x"] < "50": print spriteParams["x"] + " is less than 50"

45 is less than 50

>>> spriteParams["x"] + "20"
'4520'

>>> spriteParams["x"] + 20

Traceback (most recent call last):
File "<pyshell#44>", line 1, in <module>
spriteParams["x"] + 20
TypeError: cannot concatenate 'str' and 'int' objects
[/code:m6cjgmng]

It's just strange, is all.
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

Post » Wed Jan 16, 2008 12:05 am

[quote:2yi4f932]but when you sort y (to put it in numerical order) it affects x too[/quote:2yi4f932]
This is pretty much how C++ works, by default it uses reference semantics on arrays. I guess Python's x[:] is a way of explicitly calling the copy constructor (if Python calls it that), which is probably a good idea, so you don't accidentally end up copying around large arrays unnecessarily. In real world apps, the majority of the time arrays will be passed around by reference semantics.

As for mixing strings and ints, it's generally a grey area anyway. It works pretty similarly to Construct (you can't add or perform operations on mixed types usually, except for special operators like string concatenation &). Operations like "50" + 1 are intrinsically ambigious, do you want to get 51 or 501? Rather than allow something that may be unexpected, the general solution is to disallow it - this is not any particular language's fault. You should use explicit string or integer conversion functions to make it work how you want.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,600

Post » Wed Jan 16, 2008 12:58 am

[quote="Ashley":1r5oy4lh]As for mixing strings and ints... blah blah blah.. You should use explicit string or integer conversion functions to make it work how you want.[/quote:1r5oy4lh]

I know, my point on that was that making variables handle multiple types doesn't necessarily make them easier to use. Sure, I don't have to say x% = 12 and x$ = "dog", but the trade-off is I have to keep track of what the type is myself.

I suppose I could just get into the habit of naming my variables xN and xS to keep from getting a type mismatch.

As for the array issue, I guess it just seems like saying y = x should be explicitly stating I want to copy array x to array y. Otherwise, I'd just refer to array x. I don't really see any reason why I'd need two different names to refer to the same array, but maybe that will become clear as I continue to work with Python...
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

Post » Wed Jan 16, 2008 2:31 am

it's because arrays are called upon as objects like strings are called upon as objects

i'm not entirely sure since i haven't touched any scripting languages in a while but like strong objects, arrays have methods which you can call upon unlike integer or int, which is merely a data type
B
2
S
2
G
5
Posts: 293
Reputation: 2,236

Post » Wed Jan 16, 2008 4:24 am

The reason they do this is if you were to pass an array to a function, it's faster to pass by reference than to duplicate the array.

But this is simply how Python is by design. It's a dynamically typed language, so everything is a reference to an object. Even primitive types like int, float, and string are objects. Even the types themselves are objects, which means you can do this:
arr = [int, float, str]
B
1
S
1
G
5
Posts: 8
Reputation: 1,391

Next

Return to Open Topic

Who is online

Users browsing this forum: No registered users and 0 guests