From: Peter Otten on 8 Jun 2010 13:36 Jason Scheirer wrote: > On Jun 8, 9:37 am, Peter Otten <__pete...(a)web.de> wrote: >> Ross Williamson wrote: >> > Hi Everyone, >> >> > Just a quick question - Is it possible to assign class variables in >> > the __init__() - i.e. somthing like: >> >> > def __init__(self,self.source = "test", self.length = 1) >> >> > rather than >> >> > def __init__(self,source = "test", length = 1): >> >> No. If you are just lazy, try >> >> >>> import sys >> >>> def update_self(): >> >> ... d = sys._getframe(1) >> ... d = d.f_locals >> ... self = d.pop("self") >> ... for k, v in d.iteritems(): >> ... setattr(self, k, v) >> ...>>> class A(object): >> >> ... def __init__(self, source="test", length=1): >> ... update_self() >> ... def __repr__(self): >> ... return "A(source=%r, length=%r)" % (self.source, >> self.length) >> ...>>> A() >> >> A(source='test', length=1)>>> A(length=42) >> >> A(source='test', length=42) >> >> Personally, I prefer explicit assignments inside __init__(). >> >> Peter > > Or more simply > > def __init__(self, source = "test", length = 1): > for (k, v) in locals().iteritems(): > if k != 'self': > setattr(self, k, v) The idea was that you put update_self() into a module ready for reuse... Peter
From: Jean-Michel Pichavant on 8 Jun 2010 13:58 Peter Otten wrote: > Jason Scheirer wrote: > > >> On Jun 8, 9:37 am, Peter Otten <__pete...(a)web.de> wrote: >> >>> Ross Williamson wrote: >>> >>>> Hi Everyone, >>>> >>>> Just a quick question - Is it possible to assign class variables in >>>> the __init__() - i.e. somthing like: >>>> >>>> def __init__(self,self.source = "test", self.length = 1) >>>> >>>> rather than >>>> >>>> def __init__(self,source = "test", length = 1): >>>> >>> No. If you are just lazy, try >>> >>> >>>>>> import sys >>>>>> def update_self(): >>>>>> >>> ... d = sys._getframe(1) >>> ... d = d.f_locals >>> ... self = d.pop("self") >>> ... for k, v in d.iteritems(): >>> ... setattr(self, k, v) >>> ...>>> class A(object): >>> >>> ... def __init__(self, source="test", length=1): >>> ... update_self() >>> ... def __repr__(self): >>> ... return "A(source=%r, length=%r)" % (self.source, >>> self.length) >>> ...>>> A() >>> >>> A(source='test', length=1)>>> A(length=42) >>> >>> A(source='test', length=42) >>> >>> Personally, I prefer explicit assignments inside __init__(). >>> >>> Peter >>> >> Or more simply >> >> def __init__(self, source = "test", length = 1): >> for (k, v) in locals().iteritems(): >> if k != 'self': >> setattr(self, k, v) >> > > The idea was that you put update_self() into a module ready for reuse... > > Peter > still def __init__(self, source="test", length=1): self.source = source self.length = length is the way to go. OP's original idea is a bad idea :). Could be a problem with hundreds of parameters, but who write constructors with hundreds of parameters ? JM
From: Peter Otten on 8 Jun 2010 14:09 Jean-Michel Pichavant wrote: > def __init__(self, source="test", length=1): > self.source = source > self.length = length > > is the way to go. OP's original idea is a bad idea :). D'accord. > Could be a problem with hundreds of parameters, but who write > constructors with hundreds of parameters ?
From: danieldelay on 8 Jun 2010 14:17 you can also use : >>> class A(object): def __init__(self, **args): self.__dict__.update(args) >>> a=A(source='test', length=2) >>> a.source 'test' but this is to be used carefully because mispelling your args somewhere in your program will not raise any error : >>> A(sourc='test', lenth=2) Daniel
From: Mark Lawrence on 8 Jun 2010 16:36 On 08/06/2010 17:55, Steven D'Aprano wrote: > On Tue, 08 Jun 2010 17:24:55 +0100, Mark Lawrence wrote: > >> On 08/06/2010 17:04, Ross Williamson wrote: >>> Hi Everyone, >>> >>> Just a quick question - Is it possible to assign class variables in the >>> __init__() - i.e. somthing like: >> >> They're instance variables, not class variables. > > In the usual Python terminology, they're instance *attributes*. If a > float variable is a variable storing a float, and a string variable is a > variable storing a string, then in a language like Python where classes > are first-class objects a class variable is a variable storing a class. > > That's why Python has functions getattr, setattr and delattr, and special > methods __getattr__ etc., rather than "getvar" etc. > > I realise that other languages use "class variable" and "instance > variable" to mean something completely different, but we don't have to > emulate their mistakes... *wink* > Yes alright bloody Aussies ** n * sodit * *wink*. Not sure if this is a syntax error, but too lazy too test at an interactive prompt. Kindest regards. Mark Lawrence.
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: Question about NNTPLib Next: introducing Lettuce, BDD tool for python with Django integration |