Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
m0nk3yz
Mar 13, 2002

Behold the power of cheese!

duck monster posted:

Ah. Nice. Much more elegant than the get_foo set_foo stuff.

I'm really keen on this decorator stuff. Lots of oportunities for aspect coding type stunts.

Speaking of decorators, this popped up on pypi this morning - simple threaded/threadpool decorators:
http://pypi.python.org/pypi/threadec/

Adbot
ADBOT LOVES YOU

Hammertime
May 21, 2003
stop

m0nk3yz posted:

Speaking of decorators, this popped up on pypi this morning - simple threaded/threadpool decorators:
http://pypi.python.org/pypi/threadec/

I'm relatively python ignorant, I've only been learning it for a week or so ...

What's the point of threading in python with the GIL, excluding heavy I/O situations?

I'm ripping through project Euler and tested the multi threading of python/ironpython on one of the problems.

Single threaded python: 24 seconds
Dual threaded python: 35 seconds
Single threaded ironpython: 16 seconds
Dual threaded ironpython: 9 seconds

Note:This was an example containing pure mathematical computation.

I'm a multithreading junkie but it needs to be multi-core viable.

hey mom its 420
May 12, 2007

Yeah, the CPython implementation has the GIL. It was discussed in the beginning of the thread.
Basically good solutions if you want real threading are to use either ironpython or jython or a module like parallel python (which I really like and you should try it).

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

Hammertime posted:

I'm relatively python ignorant, I've only been learning it for a week or so ...
What's the point of threading in python with the GIL, excluding heavy I/O situations?
..snip

I actually just got done writing a fairly lengthy article talking about this (the gil+threads, etc). Here's the short version.

Python, in the cPython interpreter uses OS-level threads (pthreads for unix people) and due to this, is perfectly capable of leveraging multiple cores. However, this is blocked by the GIL, which prevents any single thread from being executed within the interpreter at once, which make the interpreter (and making extensions) simple as you don't have to manage threads/memory - the core interpreter and the GIL will do that for you.

The GIL is there to protect the interpreter's memory - not to hinder users or programmers. Ultimately, it is a cPython interpreter developer feature, not something which benefits end users (unless you are writing c-extensions). Which neatly deltas to the next point - any python code which is threaded, and only calls python code - i.e: number crunching - will run as fast (slightly slower) than the same code written single-threaded.

However, if the code you are writing uses any c-code (and this includes built-in modules written in c) which is thread-safe and uses the PyGILState_STATE/Py_BEGIN_ALLOW_THREADS macros your code will spread across processors and run faster than the single threaded implementation.

For example, 2 threads running a fibonacci calculation will run slightly slower than running the same calculation single threaded - however 2 threads performing disk I/O or socket calls will run much faster than the single threaded example.

That all being said - the GIL is a feature for the interpreter developers, and it's been stated by many people that removing the GIL will make life quite difficult for a lot of contributors and users. There has been work to remove it and I know of at least two people making a patch set for python 3000 to remove the GIL but keep the ease of use.

There are however, other options than to use the basic threading module and adding a time.sleep(.00001) (that forces the GIL to be released) and using I/O. There's actually many modules that not only mimic the threading API, but allow applications to distribute load across processors and machines.

Here's a "short list" of modules:
http://code.google.com/p/python-distributed/wiki/PythonPackagesOfNote

Check out the rest of the wiki for more information, of note is a quick run down of python GIL related articles:

http://code.google.com/p/python-distributed/wiki/PythonGILRelatedArticles

I tend to prefer the Processing module (http://pypi.python.org/pypi/processing/) given I can drop it in and quickly replace threading when I've run into scaling problems.

fake
Sep 9, 2001
I'm sorry, fake!
Love, Ozma
I still don't really understand why the GIL helps interpreter developers. They can't write threadsafe code?

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

fake posted:

I still don't really understand why the GIL helps interpreter developers. They can't write threadsafe code?

No, it means they don't have to worry about it being thread safe - no worries about memory/data corruption, deadlock issues, it keeps garbage collection simple and out of the way, etc. Yes, they can write thread safe code, but as people much smarter than I (Goetz and Eckel and others) threading is difficult to get dead-right and can lead to madness, especially when figuring out a testing matrix. Some could also argue that due to the simplicity of the C interface to python, there has been a much higher rate of third-party module developed for python. Even third party modules that bypass the GIL for concurrency.

bitprophet
Jul 22, 2004
Taco Defender
Look, between that whitespace bullshit and this inability to handle threading natively, I don't see how anyone can consider Python anything but a toy language :colbert:


...on a more serious note, to anyone else who may have been using Python for a few years now and taking it for granted: go write nothing but PHP for a month or two. You'll stop taking it for granted real quick...:gonk:

Also, the official Django book is done! Should be on shelves by mid/late December, if not earlier, I'm told :)

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

bitprophet posted:

Look, between that whitespace bullshit and this inability to handle threading natively, I don't see how anyone can consider Python anything but a toy language :colbert:

Dude, gently caress whitespace. http://timhatch.com/projects/pybraces/

Also, I pre-ordered the django book tonight.

deimos
Nov 30, 2006

Forget it man this bat is whack, it's got poobrain!

m0nk3yz posted:

Dude, gently caress whitespace. http://timhatch.com/projects/pybraces/

Also, I pre-ordered the django book tonight.

from __future__ import braces

:c00lbert:

I actually had someone argue the whole whitespace thing with me in college. I told him I didn't care and that I much prefer the way whitespace is significant in python. Same guy had some problem with his java code on one of our classes and asked me to check it... yep, he did an if, if, else and had it indented wrong and attributed the else to the wrong if.

deimos fucked around with this message at 04:17 on Nov 14, 2007

bitprophet
Jul 22, 2004
Taco Defender

m0nk3yz posted:

Also, I pre-ordered the django book tonight.

I pre-ordered it the day pre-orders were announced, which at this point must have been at least a year+ ago. Got like a half-dozen notices from Amazon since then about its release date being pushed back. But no more!

dorkanoid
Dec 21, 2004

I have a system at work where I run about 10 processes in the background using pythonw.exe - this is a bit tricky if I want to kill a specific process from the task manager, since they are all listed as, well, "pythonw.exe"

Anybody know if there's a way of "renaming" the python interpreter short of renaming the .exe/compiling with py2exe so that it's easier to kill? My current solution involves adding a XMLRPC interface to all of the processes, which kind-of solves the killing problem (unless it's stopped responding totally), but still makes it hard to see which of the processes is the one taking up 99% CPU :(

SmirkingJack
Nov 27, 2002
What is the standard for documenting your code? I get a kick out of javadoc/phpdoc. Is there a pythondoc or some equivalent way of generating HTML documentation of your API?

bitprophet posted:

...
Also, the official Django book is done! Should be on shelves by mid/late December, if not earlier, I'm told :)

Is this book you speak of The Definitive Guide to Django: Web Development Done Right or a different one?

SmirkingJack fucked around with this message at 14:50 on Nov 16, 2007

tef
May 30, 2004

-> some l-system crap ->

SmirkingJack posted:

What is the standard for documenting your code? I get a kick out of javadoc/phpdoc. Is there a pythondoc or some equivalent way of generating HTML documentation of your API?

Docstings and pydoc.

http://www.diveintopython.org/getting_to_know_python/documenting_functions.html

http://docs.python.org/lib/module-pydoc.html

No Safe Word
Feb 26, 2005

SmirkingJack posted:

Is this book you speak of The Definitive Guide to Django: Web Development Done Right or a different one?

Looks like it, based on the publication details (author and publisher). I was hoping they'd go with the same color scheme they use on the website though :smith:

bitprophet
Jul 22, 2004
Taco Defender

SmirkingJack posted:

s this book you speak of The Definitive Guide to Django: Web Development Done Right or a different one?

Yup, that's the one. Adrian and Jacob. I think the color scheme is because it's part of a technical series by the publisher, I recognize it from other books. A Django book with the Django website colors would rock, though.

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

Also note EpyDoc - it's above and beyond pydoc http://epydoc.sourceforge.net/

deimos
Nov 30, 2006

Forget it man this bat is whack, it's got poobrain!
Has anyone used pyglet yet?

I've been playing around with it and it's awesome, I think I am going to see if I can make a Visual Hub wannabe with it.

deimos
Nov 30, 2006

Forget it man this bat is whack, it's got poobrain!
Yes, I know I was the last reply to this thread, but I am currently debating a e/n post because I just got bit by one of python's more annoying "features": all directories are modules. Working on setting up my personal dev server, and goddamnit if I didn't have the hardest time setting up my virtual host with fastcgi.

Who would've thought /home/lighttpd/vhosts/domain.tld/django was a bad loving idea for putting the django projects for my site.

Yep. I just spent about half an hour wondering what the gently caress was wrong.

deimos fucked around with this message at 01:44 on Nov 18, 2007

bitprophet
Jul 22, 2004
Taco Defender
Call me stupid, but I don't get exactly what was wrong there? Never used FCGI, FWIW, always mod_python.

vvv Gotcha. That's not really a directory-as-module issue (since it should only treat directories as modules if they contain a '__init__.py' file) but more a pathing issue - the local path overrides site-packages if there's a collision, as you discovered.

bitprophet fucked around with this message at 18:27 on Nov 18, 2007

deimos
Nov 30, 2006

Forget it man this bat is whack, it's got poobrain!

bitprophet posted:

Call me stupid, but I don't get exactly what was wrong there? Never used FCGI, FWIW, always mod_python.

Not a problem with fcgi. But it was trying to import stuff in my directory called django, not the actual django stuff in my site-packages.

KelteN
Sep 12, 2001

I remember a year or two ago there was a set of videos that praised python as a language. I don't have the slightest clue as to where I saw this or who made it, but I do remember there were a few high school teachers talking about how they would teach it in their computer classes and how the students found it to be ridiculously simply to learn. If memory serves me right, the video also acted as a beginners tutorial to teach the basics of python.

Does anyone recall anything like I described? It'd be an awesome addition to this thread.

Other then that, I'd recommend skimming through the showmedo? videos regarding python. They're pretty straight forward and insightful if you can put up with the various accents the speakers have.

http://showmedo.com/videos/python

P.S. I hate the main guy Ian Ozvald. Not only does he ramble on and on in his videos, but he also tries to charge money for some of his poo poo. Which, in my mind, goes against the whole point of the open(free)-source'ness of pyton :nyd:

duck monster
Dec 15, 2004

Yeah. Threading isn't really pythons strongest point. I think most people in the Python community pretty much acknowledge that.

The reason isn't so much interpreter development, its to do with module development.

Linking C libs into Python is pretty simple, but you do have to pay attention to the garbage collection. However, things get... messy... when you start threading (As they do in C). The GIL stops this messyness by putting some nice big locks in there to keep stuff sane re Garbage collection and its ilk. You certainly apreciate it when embedding python.

But it is a problem, and one that really wants some thought put into fixing.

For what its worth, I tend to use stackless python for multithreading stuff these days. That poo poo scales like crazy. I mean, insane-loving-lala crazy. Coroutines rock.

Hammertime
May 21, 2003
stop

duck monster posted:

For what its worth, I tend to use stackless python for multithreading stuff these days. That poo poo scales like crazy. I mean, insane-loving-lala crazy. Coroutines rock.

I'm googling stackless python but I'm not turning up anything that useful.

Do you have a good link or another sentence or two? Does it get around GIL?

I *really* love python, but I also *really* love parallelism at the thread level. I'm buying the django book and I want to bring python in as a language I can leverage in case RoR pisses me off too much(full time software dev).

Edit: I'm stupid, found some good links in another search, interesting stuff, downloading it now, then I'll check out the tutorials.

Home Page (with download): http://www.stackless.com/
Tutorial/walkthrough: http://members.verizon.net/olsongt/stackless/why_stackless.html

Edit2: Well, it removes much of python's threading overhead. Doesn't solve the GIL problem though, still bound to a single core. Neat idea, seems a big waste of effort though, unless I've missed something. :(

Hammertime fucked around with this message at 11:17 on Nov 19, 2007

LOLLERZ
Dec 9, 2003
ASK ME ABOUT SPAMMING THE REPORT FORUM TO PROTECT ~MY WIFE'S~ OKCUPID PERSONALS ANALYSIS SA-MART THREAD. DO IT. ALL THE TIME. CONSTANTLY. IF SHE DOESN'T HAVE THE THREAD, SHE'LL WANT TO TALK TO ME!
I'm writing a wxPython application that deals with user-input regular expressions. Certain pathological regular expressions can take more than a few seconds (or forever) to terminate, and I want the UI to be responsive and allow the user to modify their regex during this computation. I have the regex in a separate thread, but it still seems to block the whole app while it's running.

How can I prevent a computationally intensive thread from blocking my whole app?

by the way, try matching this:
a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
against this:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

Hammertime posted:

Edit2: Well, it removes much of python's threading overhead. Doesn't solve the GIL problem though, still bound to a single core. Neat idea, seems a big waste of effort though, unless I've missed something. :(

You're correct, stackless just removes the stack: Not the Global Interpreter Lock. As has been pointed out previously, if you're trying to sidestep the GIL, look at the module I (and others) have linked too. If you're worried about the GIL + Web applications ala Django: Don't just yet. When Django Apps are served up via mod_python within Apache, the model is wildly different (given apache does use multiple cpus/cores). Most of the time in web apps you're also i/o (not cpu) bound.

m0nk3yz fucked around with this message at 13:18 on Nov 19, 2007

deimos
Nov 30, 2006

Forget it man this bat is whack, it's got poobrain!

m0nk3yz posted:

When Django Apps are served up via mod_python within Apache, the model is wildly different (given apache does use multiple cpus/cores).

Same with FastCGI and WSGI.

Monokeros deAstris
Nov 7, 2006
which means Magical Space Unicorn

I'm not sure if this deserves its own thread, but I'm not actually doing any work on this yet, so I thought I would post here to check feasibility. Any feedback people have for whether this would/should work, things to keep in mind, different ways to do things, ..., would be appreciated. If this goes anywhere, I'll start a thread about it.

Ok. I'm not really a programmer, I'm a cognitive psychologist who does some coding. At the moment, I primarily use Matlab with Psychtoolbox to deploy behavioral experiments, and Matlab proper for writing neural network simulations. I'm interested in moving to Python for both of these purposes, probably using PyGame for deploying experiments and SciPy for vectorized computations for the modeling. I'd also like to be able to run statistical analyses in Python -- basically be able to never leave Python for the entire course of an entire study.

Let me explain what is required in a typical experiment. Let's say I'm writing a simple Stroop task. I need to be able to do the following:
  • Display an instruction screen, then move on with a keypress
  • Throw a word (or several) up on the screen, possibly in color, at some very large size
  • Precisely record time elapsed since stimulus onset (time since word hits the screen)
  • Record keypresses from the user, along with reaction time (see above)
  • Spit resulting data into a CSV file for later analysis

If I were doing something like an artificial language learning task, I might not care about reaction time, but would want to be able to play sounds, and so forth. None of this is very complicated, and it seems like a library optimized for writing games should be able to handle everything I need with ease.

I guess my question is, how much work would it be to write something like the above from scratch, or at best, from snippets of reused code, using PyGame? I'm hoping that the answer is "not much", once the logic of the experiment has been determined, but I want to verify this with people who know the language and the libraries before I start sinking time into this project.

My ulterior motive in all of this is to implement this all as a project for the One Laptop Per Child program. Given a few sample experiment programs and some tutorials, I really think that kids as young as 10 or 12 could learn to conduct some fairly sophisticated cognitive psychology. Plus, how interesting would it be to learn about psychology by actually running the experiments on your friends and classmates, crunching the numbers yourself, and seeing that the results are valid? I really believe that a hands-on understanding of the human mind would be enormously valuable for more people to have, and a constructivist learning tool like the OLPC XO provides a means for even younger children to grasp some very subtle concepts and results. Since Python is the language of choice for the project, that's the language I have to learn -- though of course I've heard good things about it anyway.

So. Thoughts? Plausible? Waste of time?

Monokeros deAstris fucked around with this message at 06:14 on Nov 20, 2007

duck monster
Dec 15, 2004

Hammertime posted:

I'm googling stackless python but I'm not turning up anything that useful.

Do you have a good link or another sentence or two? Does it get around GIL?

I *really* love python, but I also *really* love parallelism at the thread level. I'm buying the django book and I want to bring python in as a language I can leverage in case RoR pisses me off too much(full time software dev).

Edit: I'm stupid, found some good links in another search, interesting stuff, downloading it now, then I'll check out the tutorials.

Home Page (with download): http://www.stackless.com/
Tutorial/walkthrough: http://members.verizon.net/olsongt/stackless/why_stackless.html

Edit2: Well, it removes much of python's threading overhead. Doesn't solve the GIL problem though, still bound to a single core. Neat idea, seems a big waste of effort though, unless I've missed something. :(

Stackless implements continuations and co-routines. Prepare for your head to explode. Basically a continuation is like a 'save point' in a game, although within the context of execution. Its sort of like saving the current state, then loving off and doing something else, so you can return later , refire execution and continue where you left off. co-routines are a species of continuation. I probably can't explain them well, so wikipedia them.

What stackless lets you do is implement 'green threads', where essentially you supply your *own* scheduler and run potentially tens of thousands of microthreads within a single thread. They scale pretty much Big-O(1) and when a thread isnt doing anything they require zero cpu time. You really can go apeshit with threads and not worry too much about the performance overhead.

A good example here would be a web server

Picture this:

code:
class testbed(servlet.servlet):
  
    def test1(self,req):
        x = 10
        self.serve("<b>Value</b> =  "+ str(x) +" "+self.simplecontinuelink('Next>'))
        x = x + 10
        self.serve("<b>Value</b> =  "+ str(x) +" "+self.simplecontinuelink('Next>'))
        x = x + 10
        self.serve("<b>Value</b> =  "+ str(x) +" "+self.simplecontinuelink('Next>'))
        return("Ah... I give up :(")
This code serves 4 web pages.
It sets x to 10 then fires off a page that says "value = x"
The page then saves its continuation and lets the server do other poo poo
When the link (Next) is pressed, it wakes up the continuation, increments x by 10, fires off another page, then falls asleep again, and so on till it gets to the return, and it closes the thread.

Now the fun part of continuations (And alas it doesnt work so well in the current stackless I *think*, is you should be able to hit the back button, and since the continuation identifier now points to the old page, it'll roll back the execution context, and re-run the code from that point in its history. Scary huh? Seaside (A smalltalk based system) uses that extensively. For multi-page forms, its erection inducing, because maintaining state gets *MUCH* easier.

It also completely flies in the face of much commonly held belief on internet best pracice. gently caress em.

The point being, is it allows you to implement cooperative multithreading in an extemely lightweight manner, that scales like a motherfucker.

And no it doesnt handle multiple cores well. Yet. I guess they are working on that. No idea about the GIL.

duck monster fucked around with this message at 08:22 on Nov 20, 2007

Hammertime
May 21, 2003
stop

duck monster posted:

Heaps of Stuff

Thanks for the explanation.

You made the threading model/scheduling and scalability really easy to digest.

As others have suggested I'm looking into process level parallelism, parallelpython and such.

POKEMAN SAM
Jul 8, 2004

Alhireth-Hotep posted:

So. Thoughts? Plausible? Waste of time?

I just got done with a project in PyGame, and this sounds really, really easy. Saving the data and dumping to CSV will be super-trivial in Python, and PyGame makes throwing things on the screen easy. Sounds are pretty easy, too. I don't know how high of a resolution timer Python has, though, if you want to be super awesomely precise (as much as the processor/OS/whatever can handle) in your timing measurements.

Monokeros deAstris
Nov 7, 2006
which means Magical Space Unicorn

Ugg boots posted:

I just got done with a project in PyGame, and this sounds really, really easy. Saving the data and dumping to CSV will be super-trivial in Python, and PyGame makes throwing things on the screen easy. Sounds are pretty easy, too. I don't know how high of a resolution timer Python has, though, if you want to be super awesomely precise (as much as the processor/OS/whatever can handle) in your timing measurements.

For my own experiments, I want to be as precise as possible, so I'll probably keep using Psychtoolbox. But as long as the timing errors don't correlate with any of the variables of interest, all they'll do is add noise to the outcome, which is a problem, but not a huge one. Thus, I think it'll probably be fine for educational purposes. Thanks, this is encouraging.

PnP Bios
Oct 24, 2005
optional; no images are allowed, only text
I've been playing around with python in terms with game-development and I am very impressed with what I can do with it. Using a dictionary to manage various states, memory management, abusing a static class to manage game variables, it's all good :D.

here is a paste bin of my current breakout style project. The ball bounces, use the arrow keys to move around, and F1 and F2 to switch the states. You also need pygame.

http://rafb.net/p/DpRL0514.html

duck monster
Dec 15, 2004

Yup. Thats the magic of python. Dictionarys, lists and tuples. So simple, and yet so expressive. Sure you can do it in other languages, but python makes it so elegant.

Also, Py-game is a really fun library. Aeons ago I wrote a little game that I never finished based on a really spastic take on eve-online (Called something like "goonie lagsplot 2d" or something absurd). Although I never really bothered beyond about 4 or 5 pages of code it was loving fun to write what I wrote, and the prototype was actually kind of fun.

I could see it being very easy to write a 'real' 2D game in it.

What would be *real* cool would be a python version of Delphis old GLScene library. Open GL coding gets really drat cool when its turned into a first class OO library.

deimos
Nov 30, 2006

Forget it man this bat is whack, it's got poobrain!
I would personally rewrite:
code:
class gameVars():
 
	# the paddle sub-class
	class paddle():
		x = 0
		y = 0
		w = 128
		h = 16
 
	# the ball sub-class
	class ball():
		x = 0
		y = 0
		vx = 0
		vy = 0
		radius = 8
 
	# the arena sub-class
	class arena():
		w = 512
		h = 640
		score = 0
		lives = 0
		round = 0
		balls = []
as
code:
gameVars = {
	'paddle' : {'x':0,'y':0,'w':128,'h':16 },
	'ball' : {'x':0, 'y': 0,'vx':0,'vy':0, 'radius' : 8},
	'arena' : {'w' : 512,'h' : 640,'score' : 0,'lives' : 0,'round' : 0,'balls' : [] }
}

duck monster posted:

Yup. Thats the magic of python. Dictionarys, lists and tuples. So simple, and yet so expressive. Sure you can do it in other languages, but python makes it so elegant.

Also, Py-game is a really fun library. Aeons ago I wrote a little game that I never finished based on a really spastic take on eve-online (Called something like "goonie lagsplot 2d" or something absurd). Although I never really bothered beyond about 4 or 5 pages of code it was loving fun to write what I wrote, and the prototype was actually kind of fun.

I could see it being very easy to write a 'real' 2D game in it.

What would be *real* cool would be a python version of Delphis old GLScene library. Open GL coding gets really drat cool when its turned into a first class OO library.

Look into pyglet. OpenGL is pretty much a first class citizen. Not to mention a :swoon: of a positional sound library.

deimos fucked around with this message at 02:40 on Nov 21, 2007

SmirkingJack
Nov 27, 2002
Ok, I just don't get it.
code:
class test(object):
    
    var1 = ""
    var2 = []
    
    def change(self, newString):
        self.var1 = newString
    
    def add(self, newItem):
        self.var2.append(newItem)

If I do something like
code:
a = test()
b = test()

a.change("foo")
b.change("bar")

a.add("pillow")
b.add("talk")

print a.var1
print b.var1

print a.var2
print b.var2
The output is
code:
foo
bar
['pillow', 'talk']
['pillow', 'talk']
Why is var2 shared between instances but var1 is not? And why is var2 shared to begin with? So far Python strikes me as an odd language.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.
Try "print test.var1" and "print test.var2". (It'll be more clear if you initialize var1 to "default" instead of "".)

Here's what's happening:

code:
class test(object):
    
    var1 = ""  # creates a class variable (static in C++), test.var1
    var2 = []  # creates a class variable test.var2
    
    def change(self, newString):
        self.var1 = newString  # creates instance variable self.var1
    
    def add(self, newItem):
        self.var2.append(newItem)
        # no assignment statement - only modifies existing variable
        # since there is no self.var2, falls back to test.var2 and modifies that
You can see this in action with:

code:
a = test()
b = test()
a.var2 = ["new", "list"]
a.add("mmm")
b.add("pancakes")
print a.var2
print b.var2
Although of course that's terrible programming style.

If you want to be sure an instance variable is initialized before calling add, do it in __init__:

code:
class test(object):
  def __init__(self):
    self.var1 = ""
    self.var2 = []
  ...

FuraxVZ
Dec 18, 2004
Incredibly Unimaginative

I'm looking to get into Python a bit; since I learn best from paper books, any recommendations? And yes, yes, I know, you learn by doing not reading, but I like books for grokking languages. Something with some good depth and the spirit of the language would be great.

The online Diving into Python is pretty good, and I was thinking of getting the dead-tree version. Is there better?

mantaworks
May 6, 2005

by Fragmaster

FuraxVZ posted:

The online Diving into Python is pretty good, and I was thinking of getting the dead-tree version. Is there better?

The only suggestion I can make is take one that uses version 2.5. DIP is written for 2.3 and as such has a kind of roundabout way of dealing with things that can be more elegantly written in 2.5.

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

FuraxVZ posted:

I'm looking to get into Python a bit; since I learn best from paper books, any recommendations? And yes, yes, I know, you learn by doing not reading, but I like books for grokking languages. Something with some good depth and the spirit of the language would be great.

The online Diving into Python is pretty good, and I was thinking of getting the dead-tree version. Is there better?

The latest edition of Core Python Programming is really good. Covers 2.5 to boot.

Adbot
ADBOT LOVES YOU

deimos
Nov 30, 2006

Forget it man this bat is whack, it's got poobrain!

m0nk3yz posted:

The latest edition of Core Python Programming is really good. Covers 2.5 to boot.

I second this, I particularly like Core Python's treatment of variables and memory.

  • Locked thread