How can I make a sentinel value NOT be initialized in a class/method - OOP?
python at lucidity.plus.com
Sat Jan 14 16:13:26 EST 2017
[Replying to direct email. David - please respond to the list so that
you can receive other people's suggestions also]
On 14/01/17 15:30, David D wrote:
> 1) No this is not homework, I am doing this all on my own.
In that case I apologise - I gave you the benefit of the doubt though as
I was only half-suspicious. FWIW, listing specific constructs that must
be used is often a thing that a tutor would write in an assignment
(because the purpose of the assignment is to get you to learn about
those particular constructs). That's what raised the flag for me. Anyway ...
> 2) I am changing the class to cars.
> 3) A second question arose that I thought would work, but I am getting
> this error :*can't assign to a function call*
> What I am trying to do is this pseudo code :
Firstly, there's little point in posting pseudo code if you are
reporting a specific compiler or run-time error. The errors you get are
very specific to your code and in some cases are caused by *subtle*
things in your code. We generally need to see the actual code (or a
cut-down example) that allows the subtleties to be described to you.
> count= 0
> class car
> initialize all of the values and put self so they are available to the
> entire class
> def __init__(self, model...)
> self.mode=model etc
> while loop
> input model =what is the model
> input serial = what is the serial
> input doors = how many doors
> count = count + 1
> #create the instance/object
> mycar (count) = car(model, serial, doors)
> input do you want another car in the database?
> if yes
> if no
> The issue is that when creating an object/instance, python won't let me
> use this syntax of having -- car(count) which would give me multiple
> objects (car1, car2, car3 etc) with the count variable. I am not
The syntax "foo(spam)" will *call* the function "foo" and pass it the
value "spam". It doesn't make any sense to _assign a value_ to a
function call operation (you're effectively trying to assign a value -
the thing after the '=' - to another value - the return value of the
So, what is "mycar"? How do you create it?
In Python, the built-in structure for a group of objects which can be
dynamically extended with new entries in the way you want is a list.
Create an empty one with:
mycar = 
mycar = list()
Lists have an 'append()' method which will add the parameter it is
called with to the end of the list object it is called on:
mycar.append(car(model, serial, doors))
You don't have to worry about keeping track of 'count'. Lists will grow
dynamically as you add things. Use "len(mycar)" to see how many items it
holds and "for vehicle in mycar:" to step through them all or "mycar[n]"
to address as specific entry.
From the Python prompt, type "help(list)"
Hope that helps.
More information about the Python-list