Forgot my wrapup of example! Sorry!
Steve Holden
steve at holdenweb.com
Mon May 2 22:32:44 EDT 2005
GMane Python wrote:
> So, I forgot the last part of my example that might gel in your mind why
> Objects are useful in certain situations. Ok so you maybe followed my
> example of the shopping cart. Let's just forget for a moment the use for
> shopping carts is for websites. Let's just say you were going to write the
> lines directly into Python, like maybe at the IDLE interpreter. Like maybe
> you're testing the functionality of the routine for correctness, not actual
> implementation.
>
>
[good introductory class stuff snipped]
>
>
> In a class, you can have a variable 'total' and you can have a variable
> 'self.total'. The first is just accessible from within the structure of the
> class. The latter is accessible from outside the class. So, using a class
> with the variable self.total, you can from outside the class say:
>
>
>
> print ShoppingCart["Dave"].total
>
>
>
> to get the value of the variable self.total. (That had me buggered for a
> while ..... )
>
Thanks for the smile this gave me. We all get to this stage periodically
when programming.
>
>
> If you can wrap your mind around this, you're well on your way to using OOP
> I believe. If not, , don't give up. I'm just a python/programming newbie
> and maybe missed the boat completely with my posting.
>
Well maybe you did, but you actually managed to demonstrate important
parallels between namespaces and dictionaries in Python.
>
>
> Another quite important part I didn't even mention was sub-classing. That's
> taking a class, and 'inheriting' it's code to your class as a base, then you
> can re-write or adddifferent methods. But, first I think it's important to
> understand how to have several instances of a single class running in
> memory, each with different values for the defined variables. I'll let
> someone else talk about that.
>
As indeed they almost inevitably will :-)
regards
Steve
--
Steve Holden +1 703 861 4237 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
More information about the Python-list
mailing list