Setting attributes in __init__ from args was: Strange error in Python code

  • From: "R Dinger" <rrdinger@xxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Mon, 14 Dec 2009 17:34:02 -0800

If all the class attributes have the same names as the arguments to __init__ 
and the __init__ function does nothing other than set attributes to the args, 
then write the following function:

def attributesFromArgs(d):
  self = d.pop('self')
  for n, v in d.items( ):
    setattr(self, n, v)

Then when writing a class, the __init__ function only needs one line:

class C():
  def __init__(self, a, b, c=3):
    attributesFromArgs(locals())

The attributesFromArgs function will pop the self arg out of the locals 
dictionary and assign the name-value pairs in locals to the class.  Creating an 
instance of class C above:

x = C(1,2)

Will produce a C object with attributes:
x.a = 1
x.b = 2
x.c = 3

The above function may be useful if you write many classes with long 
initialization functions.

Richard
----- Original Message ----- 
  From: Ken Perry 
  To: programmingblind@xxxxxxxxxxxxx 
  Sent: Sunday, December 13, 2009 3:45 PM
  Subject: RE: Strange error in Python code


  I think you are miss understanding.  The difference though.  The declarations 
in your __init__(parameters) are local variables.  Note they don’t have a self 
on them.  So what you are really setting is a local variable for only using in 
the __init__ function.  The variables with self on it are actual members of the 
class and thus what you have to do is set the member variable to the local 
variables value like 

   

  Def __init__(self,bah):

    Self.bah=bah

   

  Ken

   

  From: programmingblind-bounce@xxxxxxxxxxxxx 
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Storm Dragon
  Sent: Sunday, December 13, 2009 3:19 PM
  To: programmingblind@xxxxxxxxxxxxx
  Subject: Re: Strange error in Python code

   

  Hi,
  I must have remembered incorrectly lol.  All my other code does have 
declarations in the __init__ method as well as in its parameters.
  Storm

         

 -- Thoughts of a Dragon:http://www.stormdragon.us/What color dragon are 
you?http://quizfarm.com/quizzes/new/alustriel07/what-color-dragon-would-you-be/ 

  On Sun, 2009-12-13 at 12:03 -0800, R Dinger wrote:



   

    I have never seen any method of initializing vars through the call 
signature only. 

      

    Are you certain you are remembering correctly?  And if so, can you give an 
example? 

      

    Richard 

      ----- Original Message ----- 

      From: Storm Dragon 

      To: programmingblind@xxxxxxxxxxxxx 

      Sent: Sunday, December 13, 2009 9:49 AM 

      Subject: RE: Strange error in Python code 

       

      Hi,
      I am very confused.  I thought the vars were getting initialized in the 
__init__(self, now = time.time(), clicks = 1): part.  That's why I had pass in 
that method.  However, your suggestion fixed the problem I was having.  I had 
to move both variables though.  So now it reads:
          def __init__(self):
              self.clicks = 0
              self.now = time.time()
      The thing that confuses me is I have written code with variables declared 
in the () before and they worked.  Why would this time be different?
      Thanks
      Storm

             

 -- Thoughts of a Dragon:http://www.stormdragon.us/What color dragon are 
you?http://quizfarm.com/quizzes/new/alustriel07/what-color-dragon-would-you-be/


           

      On Sun, 2009-12-13 at 12:30 -0500, Ken Perry wrote: 





       

      You need to put self.clicks =0 in the __init__ function because you’re 
trying to do this

       

      None+=1

       

      You have to initialize it first 

       

       

       If you were just setting it like

       

      Self.clicks=0 

       

      Then you could do it in the function but that would not be what you want. 
 So initialize it and your done.

       

      Note you don’t need __init__ if you are just doing pass

       

       

       

      From: programmingblind-bounce@xxxxxxxxxxxxx 
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Storm Dragon
      Sent: Sunday, December 13, 2009 11:26 AM
      To: programmingblind@xxxxxxxxxxxxx
      Subject: Re: Strange error in Python code




      Hi,
      Lol it seems so obvious now. Forgetting to put self.variable can be 
detrimental.
      I got all of that fixed and am still getting an error about no attribute 
called now, but it is there, I double checked.  Here's the revised code:
      import time
      class countPresses:
          def __init__(self, now = time.time(), clicks = 1):
              pass

          def clickCount(self):
              if time.time() <= self.now + 0.75:
                  self.clicks += 1
              self.now = time.time()
              if self.clicks > 2: self.clicks = 1
              return self.clicks

      test = countPresses()
      print str(test.clickCount())
      print str(test.clickCount())

             

  -- Thoughts of a Dragon:http://www.stormdragon.us/What color dragon are 
you?http://quizfarm.com/quizzes/new/alustriel07/what-color-dragon-would-you-be/ 


      On Sun, 2009-12-13 at 07:30 -0800, R Dinger wrote:



       

      Your calls to clickCount do not have parens e.g. clickCount(), which is 
required for a function call.  Also you have an undefined local variable in 
click count named now, which is used prior to having a value. 

        

      Richard 

      ----- Original Message ----- 

      From: Storm Dragon 

      To: programmingblind@xxxxxxxxxxxxx 

      Sent: Sunday, December 13, 2009 6:15 AM 

      Subject: Strange error in Python code 

       

      Hi,
      I may have gone about this the wrong way. I wanted a simple way to tell 
if a button has been pressed twice.  My class gives a strange error though.  I 
wrote a quick bit of code to test it.  It just calls the method twice in 
succession so it should get 1 then 2 for output.  Instead, it says:
      <bound method countPresses.clickCount of <__main__.countPresses instance 
at 0x7f0a97e78c68>
      Here's the code:
      import time
      class countPresses:
          def __init__(self, clicks = 1, now = time.time()):
              pass

          def clickCount(self):
              if now + 0.75 >= time.time():
                  self.clicks += 1
              now = time.time()
              if self.clicks > 2: self.clicks = 1
              return self.clicks

      test = countPresses()
      print str(test.clickCount)
      print str(test.clickCount)

      Thanks
      Storm

             

  -- Thoughts of a Dragon:http://www.stormdragon.us/What color dragon are 
you?http://quizfarm.com/quizzes/new/alustriel07/what-color-dragon-would-you-be/



           


       

Other related posts:

  • » Setting attributes in __init__ from args was: Strange error in Python code - R Dinger