[Tutor] like setattr but for methods...

Lloyd Kvam pythontutor@venix.com
Sat May 24 09:43:02 2003


You have one reply.  I am going to guess that you were looking for a different
kind of answer.

class X:
	def __init__(self):
		self.attr1 = 1
		self.attr2 = "test"
		self.attr3 = [1,2,3,4,5]

Y = X()
print Y.attr1
 >>> 1

__init__ can provide initial values for an instance.  You can also provide
more arguments after self if necessary.

Typically people use Upper Case for Classes and lower case for an instance.

Hopefully one of these replies answers your question.

(When starting a new thread, it's better not to reply to an existing message.
Even when you change the subject, the mail headers will place the message into
the thread you replied to.  I think that's what happened here.)

ali mangaliag wrote:
> if i have a class, X, and an instance of X called Y...
> i can add attributes to Y in two ways:
> 
> a.)
> Y.attr1 = 1
> Y.attr2 = "test"
> Y.attr3 = [1,2,3,4,5]
> 
> b.)
> setattr(Y,"attr1",1)
> setattr(Y,"attr2","test")
> setattr(Y,"attr3",[1,2,3,4,5])
> 
> is my understanding of this correct???
> 
> my second question is... is there a way for me to define a method like the
> way i did (or anything similar) with the attributes above???
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-443-6155
fax:	801-459-9582