[Tutor] Need all values from while loop - only receiving one
mats at wichmann.us
Sun Jul 8 12:42:21 EDT 2018
On 07/07/2018 03:35 PM, Alan Gauld via Tutor wrote:
> On 07/07/18 18:46, Daryl Heppner wrote:
>> I'm stepping back to simply return the DealID from the XML using the
>> concept of a class. My code results in exit code 0 but doesn't print
>> any results. Could you give me a hint where I'm missing something?
> You have an issue which has shown up in several of the code
> samples you have sent in that you don't assign values to variables.
> In your code below Deal is a class. To use it you must create an
> instance, which you do at the bottom.
> But the methods defined in the class do not execute until
> you call them from the instance. So in your code below you
> create an instance, but because it is not stored anywhere
> (with a variable) you do not execute any of its methods
> (except init) and Python, seeing that it is unassigned,
> immediately deletes the object. To use an object you must
> store it someplace; either by assigning it to a variable
> or inserting it into some kind of collection
I don't know if this is preaching about something you already understand
very well; if so, sorry, just ignore this.
It's not always instantly obvious that in Python operations create
objects. This is not quite the same as the Object Oriented Programming
description of an object, which might tell you "an object is an instance
of a class". From the surface viewpoint, anyway, Python has objects
that don't look like class instances - at least, not instances of
classes which you are aware of being classes, or which use the
class-calling syntax like Foo() to create them.
To do anything with an object, you need a reference to it, so you assign
a name, store it in a collection, etc. A simple numeric operation
creates an object, you can see its "identifier" with the id() function
(btw the value returned by id() is not intrinsically meaningful, but it
does show you when names refer to the same or different objects):
>>> x = 2 + 7
You can assign additional names for objects:
>>> y = x
that object, which has a value of 9, now has two references.
A function definition creates an object, and assigns the name from the
definition to the object:
>>> def foo(q):
You can make additional names for a function object as well, it's just
>>> z = foo
It's callable by either name:
The same is true of a class definition, it creates an object and binds
the name to it. When you call the class object, it creates an instance
As Alan says, Python reference-counts objects, so you need to hold on to
at least one reference as long as you want the object to live.
More information about the Tutor