[Tutor] the class struggle

Rob McGee i812@iname.com
Fri, 30 Nov 2001 15:04:31 -0600


First off, thanks to all who replied. I'll try to cover everything in a
single reply here. Sorry if it's a bit disjointed. I've made a lot of
progress, but there's still one remaining item of confusion -- see down
at the end of this message.

Whoa, that's bad Internet manners. Here's the question:
    How, in a class __init__ function, can you get the name of the
    instance?
A code sample (tested this time :) and further explanation of what I'm
trying to do is down at the end. But if you have a quick answer to this
question you probably don't have to read (nor quote) all the way.

On Thu, Nov 29, 2001 at 04:36:59PM -0800, Kirby Urner wrote:
> >{code}
> >import random
> >
> >def myObject(name):
> >   variable = name
> >   anotherOne = anotherValue
> >   if name in someList:
> >     nextVar = this
> >     lastOne = that
> >   else:
> >     nextVar = random.randrange(10, 70)
> >     lastOne = random.randrange(5, 15)
> >{/code}
> 
> 
> Rob, this function is not self contained as written.

Eek! You're right. I'm sorry. I tried to simplify my code and ended up
oversimplifying. This time around I'm going to simplify the *questions*
I'm asking. :)

> You've defined a lot of the variables in the shell,
> off camera (so to speak) and these are available as
> global variables.  You also appear to be assigning
> to globals, versus returning values.  That's called

Actually they were hard-coded values. I used variables in my example,
but the real code had values. And yes, the function was assigning global
variables. I think it would be much better to have a class instance's
namespace.

> relying on side-effects and its a major ingredient
> in messy, hard-to-debug code.  Not to worry.

Yes, I know I've got a long way to go. {aside} This isn't the first time
I've tried to take up programming. When I was running DOS 5 on a 8086, I
had a terrible time trying to write a little game in QBasic (ever hear
of "MasterMind", a code-breaking game? This was a solitaire, text-mode
version of it.) I finally got it but was really burned out. Toward the
end of my Windows days I played with MSVB4, but I got bored with it. Now
in python the light is really starting to come on. :) {/aside}

> >That works. I got a complete and working (although not very polished)
> >program using a function like that. It's worthy of the Useless Python
> >site, for sure. :)
> 
> I think as a standalone, it's not ready for Useless, because

I guess I had a different understanding of the mission of Useless. :)

> of it's heavy reliance on stuff you've defined out-of-scope.

This was actually a subordinate function nested inside another one. Not
that it really matters as to what I'll be asking below, but I'm just
trying to explain what was going on. There was an index number and a
list coming from the parent function. The index is incremented, and the
value from the list is remove()'ed so that it won't be available for the
next pass (values must be unique.)

I realize that nesting functions probably isn't the best way to do it.
The index is incremented in a for loop. But I'm not sure of how else I
might maintain my list? ISTM it has to remain out-of-scope.

> Also, it's not documented (gotta have comments explaining
> the purpose of the function, before it goes to the internet).

Again, this was an oversimplification. It's liberally commented in "real
life". :)

> >{code}
> >import random
> >
> >class myObject(name):
> >   def __init__(self, name):
> >     variable = name
> >     anotherOne = anotherValue
> >     if name in someList:
> >       nextVar = this
> >       lastOne = that
> >     else:
> >       nextVar = random.randrange(10, 70)
> >       lastOne = random.randrange(5, 15)
> >{/code}
> 
> The first problem here is you're saying your class inherits from
> some other parent class named 'name'.  Unless you've actually

Yes, all of you caught that. I was indeed getting confused between
    def function(values, passedToIt):
and
    class Class(ParentClass):
I thought I was passing values to the class that way. That's one part of
my class struggle completed successfully. :)

> >That doesn't work. "SyntaxError: invalid syntax" on the "if"
> >(caret goes under the "f".) I tried adding "self." in front
> >of the class instance variables just for fun. Same thing.
> 
> I don't understand this error exactly.  I can't reproduce it.
> Are you in IDLE, and do your key words colorize?  Is your
> 'if' orange?

I finally found it. Yes, I was using IDLE. But IDLE didn't make me use
")" to close off a function call. :) Basically it was like this:
    random.randrange(start, stop
without the closing parenthesis. It didn't jump out at me because the
stop value was itself a product of two functions: I had two ("))") but
needed three.

IOW PEBKAC, never mind, sorry. :)

> It could be a consequence of trying to inherit from
> 'name' as a parent class.  You may have 'name' defined
> as a global elsewhere...

Is there an easy way to clear out the IDLE session between runs of a
script?

And now for something completely different ...

... not really different, but I've been awaiting an opportunity to say
that here. :) For those of you who read above, here's a repeat:
    There's one little question which I think may be the only thing left
    I'm not understanding. How, in a class __init__ function, can you
    get the name of the instance?

{code}
surnames = ('Marx', 'Engels', 'Lenin')

class Struggle:		# no parent class! :)
  def __init__(self, firstnames):
    self.firstname = firstnames.pop()
    if self.firstname == 'Vladimir':
      self.nationality = 'Russian'
    else:
      self.nationality = 'German'
#   self.surname = self

firstnames = ['Vladimir', 'Friedrich', 'Karl']

for x in surnames:
  exec(x + ' = Struggle(firstnames)')
{/code}

The "self.surname" thing doesn't do it. I get this:
    <__main__.Struggle instance at 0x80db334>
What I want to do is a different test inside the class definition. Like
    if len(surname) == 5:	# 'Lenin'
      self.nationality = 'Russian'
What would work in the place of "surname"? Or can this be done?

See, what I'm wanting is to use fixed values for class instances where
the name is in a list, and random values for all other instances. I can
see workarounds now, such as passing more arguments to the class
instantiation, but it seems simpler to me to do it the way I had wanted
originally (which was how I did it in my function.)

Thanks again, all, and I'm sorry about confusing you all with my code
example. :)

    Rob - /dev/rob0