newbie question

Martijn Faassen m.faassen at vet.uu.nl
Mon May 28 18:00:32 EDT 2001


Jasonbail <jasonbail at aol.com> wrote:
>>> I'm fairly new to programming,
>>
>>> My questions is, is there anyway to create a reference to a pre-existing
>>> variable by using variables?
>>
>>> for example if I have a variable daily_count
>>
>>> can I access that variable by using "daily_" and "count" thats stored in
>>other
>>> variables?
>>
>>Since you are new to programming, I think the best answer here would
>>be to ask you some more questions. The most important one in these cases
>>is always: 
>>
>>  Why would you want to do that?

> Well let me first say that I have a tendency to leap first and then try to
> figure out if I will be able to land safely.

Not a bad tendency if you want to learn programming.

>     The idea behind my post was that I was in the process of creating a simple
> app when I noticed that the majority of the code I was using to create the gui
> was nearly identical. It stood to reason then that I could I define a function
> that I could then use to configure the widget and that all I had to do was pass
> along the name of the widget that would need configuring.

That is good programmer thinking; programmers don't want to repeat themselves
because we're lazy, and there happen to be good arguments involving 
maintenance of this code as well (if you have the same construct in a lot
of places, it's a lot of work changing them all and you may make a mistake
easily; if it's in a single place that can't happen).

> And to do that I
> would need to store the name in a variable to pass along to the function, which
> it would then configure and at a later point I could re-configure to my hearts
> content.

You could also pass a reference to the widget object (I'm not sure what
GUI toolkit you're using) to your own function, and then work on the
widget object that way, by calling its methods. Variables in python can point
to widget objects as much as they can point to string objects, after all.

>     Of course I don't need to do that. It took me almost the entire night to
> wrap my head around the thought that I could create a method and that by
> creating an instant of the method I would then be able to do what I originally
> planned on doing. (sorry if I butcher the terminology here, I'm still working
> on that) Some of this is very confusing at first.

Creating an instance of a method doesn't sound like terminology I 
understand, no problem. :) It's understandable things are confusing at
first, no problem. 

>    The reason why I didn't go to far in to explaining what I was doing is that
> because I knew I was new to this, I knew that there was probably a better way,
> but that wasn't what I was looking for. Sometimes people will try answering my
> problem rather then my question. Which I dislike because regardless of my
> intent I still want to know the answer to my question (which in this case
> several people, I'm happy to say, have answered.)

It's just that your question was so vague to me there wasn't a single
good answer possible, though I'm glad people did supply you with some
answers.

>>In Python, variables (attributes, etc, in python terminology, names) are
>>always references to *objects*. The only way you can approach objects is
>>to go through these names in some fashion. The objects contain the data
>>and define the behavior, however. The names may refer to one object now,
>>and another in the future. Multiple names can refer to the same object.
>>This distinction between names and the objects they refer to is very
>>important in Python, so you make sure you clearly understand this. It
>>is not extremely difficult to understand and to deal with once you
>>understand, but it is a common problem for many that are new to Python.
>>

>  I didn't quite see that until you pointed it out. After reading your post I do
> see your point and I think I was probably to reserved here. I will try to be a
> little bit more verbose on my questions in the future. Thanks for the feedback,
> and I will take you up on the clarification as soon as I understand enough to
> form a coherent question. :)

As long as you learned anything, it was a good question, clear or not. :)

Good luck!

Regards,

Martijn
-- 
History of the 20th Century: WW1, WW2, WW3?
No, WWW -- Could we be going in the right direction?



More information about the Python-list mailing list