[Tutor] Proper uses of classes
Mark Lawrence
breamoreboy at yahoo.co.uk
Fri Apr 5 10:48:37 CEST 2013
On 04/04/2013 18:01, frank ernest wrote:
> Sorry about the indentation guys my mail service atomatically removed it
> when I pasted the text (I tried to fix it by hand but it was hard to
> tell if I was sucessful.)
> I was tring to create a list object (self) and change it using the list
> methods and some additional ones I will define in my class.
> I wanted to create some varibles which I pass to most if not all of my
> methods. Two of my variables are to be passed to the class apon creation
> of the object and then they in turn are to be passed to all or most of
> the methods I create (a, b in this case.) If I make a, b nonlocal I get
> the message that they are already nonlocal.
> I will need for debuging purposes to print my object in random places in
> my class.
> If I did this:
>
>> self.a = a
>
> What would that create (I'm thinking local variable?)?
If the line is in a class method you'd create a (using Python speak)
name 'a' for every instance of your class.
>
> class alist():
> def __init__(self, b, a):
> self.mylist = list()
> self.mylist.append(b)
> self.a = a + b
> def appendit(self):
>
> #I need to get a in hear without passing
>
> #it in; it must come from the init method.
> self.mylist.append(self.a)
>
> #(I hope I've updated the code well, I can't test it from this computer.)
>
> PS: This is not the real code that I'm Emailing: I thought I'd be better off with some simpler code that produces the same errors.
>
As I see it all you gain from the appendit method is the ability to
delay adding 'a' to mylist until you've done some other processing. Is
this what you're trying to achieve, if not can you give us more detail?
That way we can help you code up your requirements.
--
If you're using GoogleCrap™ please read this
http://wiki.python.org/moin/GoogleGroupsPython.
Mark Lawrence
More information about the Tutor
mailing list