#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Atlantic City, NJ
    Posts
    327
    Rep Power
    13

    Method Attributes


    I think I'm missing something big here and I'm sure the answer is very easy. I can't seem to get attributes from another method inside the same class. For instance:

    Code:
    class glsa:
    
    	def getglsa(self):
    		"""Returns a list of any 
    		possible security updates"""
    		self.secups = commands.getoutput('glsa-check -ln 2> /dev/null')
    		self.secups = self.secups.split('\n')[4:]
    		return self.secups
    
    	def glsanum(self):
    		"""Returns the number of
    		possible security updates"""
    		self.numups = len(self.secups)
    		return self.numups
    Now this throws an error when I try to use the 'glsanum' method saying it can't find the 'secups' attribute. I'm not understanding why.
    I'll learn this stuff someday.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    78
    Rep Power
    10
    Originally Posted by Shiner_Man
    I think I'm missing something big here and I'm sure the answer is very easy. I can't seem to get attributes from another method inside the same class. For instance:
    ...
    Now this throws an error when I try to use the 'glsanum' method saying it can't find the 'secups' attribute. I'm not understanding why.
    You have neither set a default self.secups in __init__(), nor have you called getglsa.

    --OH.

    Comments on this post

    • CyBerHigh agrees : ya, I agree
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Atlantic City, NJ
    Posts
    327
    Rep Power
    13
    I see. So I have to create the secups attribute within __init__ in order for it to be created when every instance is called right?

    Code:
    class glsa:
    
            def __init__(self):
                    self.secups = commands.getoutput('glsa-check -ln 2> /dev/null')
    		self.secups = self.secups.split('\n')[4:]
    
    	def getglsa(self):
    		"""Returns a list of any 
    		possible security updates"""
    		return self.secups
    
    	def glsanum(self):
    		"""Returns the number of
    		possible security updates"""
    		self.numups = len(self.secups)
    		return self.numups
    So there is no limitation as to how complex the code is within __init__ correct? You don't have to simply do 'name = object' as most of the examples show?

    Thanks a lot!
    I'll learn this stuff someday.
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Nope. Theres no real limit to how complex you make the code in __init__. With the exception being you that __init__ can't return a value for obvious reasons.

    That said I would stick to using it to initialize the instance because that's what it's for and it's what other programmers expect. How you initialize the values is entirely up to you .

    You can or course do without __init__ entirely but you have to remember to call your custom setup method before any of the methods that use the values it defines.

    Code:
    >>> class A:
    ...     def __init__(self, val):
    ...         self.val = val
    ...     def printVal(self):
    ...         print self.val
    ...
    >>> a = A('Hello')
    >>> a.printVal()
    Hello
    >>>
    >>> class B:
    ...     def setup(self, val):
    ...         self.val = val
    ...     def printVal(self):
    ...         print self.val
    ...
    >>> b = B()
    >>> b.printVal()
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      File "<stdin>", line 5, in printVal
    AttributeError: B instance has no attribute 'val'
    >>> b.setup('Bye')
    >>> b.printVal()
    Bye
    >>>
    Note: how the b instance is created in the example above using the "b = B()" syntax. The brackets at the end of the class are really just a shortcut for calling the classes __init__ method. Since we didn't defined __init__ in the B class you might wonder whats going on. In Python all new classes inherit from the built-in object class, which has it's own set of methods including __init__ .

    You can get a list of all the methods that object defines using the dir() function.

    Code:
    >>> dir(object)
    ...
    Hope this helps,

    Mark.
    Last edited by netytan; March 10th, 2005 at 06:13 AM.
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Atlantic City, NJ
    Posts
    327
    Rep Power
    13
    Yes that helps a lot. Thank you.

    I was unclear because the examples in the book I'm using are very brief and only show simple assignments in the __init__ method.

    I'm asking a lot of questions because I don't want to develop any bad habits since I'm just starting.

    Again, thanks a lot.
    I'll learn this stuff someday.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    78
    Rep Power
    10
    Originally Posted by Shiner_Man
    Code:
    class glsa:
    
            def __init__(self):
                    self.secups = commands.getoutput('glsa-check -ln 2> /dev/null')
    		self.secups = self.secups.split('\n')[4:]
    
    	def getglsa(self):
    		"""Returns a list of any 
    		possible security updates"""
    		return self.secups
    
    	def glsanum(self):
    		"""Returns the number of
    		possible security updates"""
    		self.numups = len(self.secups)
    		return self.numups
    You shouldn't use explicit getters and setters in python - you should just access the property directly. If you need a computed value, you should STILL access the property directly - and use the property() function to wrap your getters and setters:

    Code:
    class GLSA(list):
        def __init__(self):
            glsa = commands.getoutput('glsa-check -ln 2> /dev/null')
            self.extend(glsa.split('\n')[4:])
            self._computed_prop = None
    
        def get_computed_prop(self):
            return "!!%s!!"%self._computed_prop
        def set_computed_prop(self, val):
             self._computed_prop = val
        computed_prop = property(get_computed_prop, set_computed_prop)
    
    >>> glsa = GLSA()
    >>> print len(glsa)
    ... somenum
    >>> print glsa
    ... [somelist]
    >>> glsa.computed_prop = glsa
    >>> print glsa.computed_prop
    ... !![somelist]!!
    --OH.

IMN logo majestic logo threadwatch logo seochat tools logo