python - Can attributes be assigned anywhere within a class? -
can attributes assigned anywhere within class? if so, how scope rules works each of following cases?
class greatcomposers(object): def __init__(self, name, birthday, instrument): # attributes assigned in __init__ self.name = name self.birthday = birthday self.instrument = instrument def setfullname(self) # attributes assigned in other class methods self.fullname = self.name + self.birthday self.job = self.instrument + 'ist' # attributes assigned outside functions self.nationality = 'german'
no, doesn't work @ class scope (self.nationality = 'german'
in example), there no name self
in scope @ point. , doesn't work in other cases because methods or self
parameter somehow special. attributes can assigned anywhere have reference object. includes methods, other code has access object in question.
Comments
Post a Comment