Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Firstly you're using old style classes. You <strong>really, really</strong> should be using <a href="http://docs.python.org/glossary.html#term-new-style-class">new style classes that inherit from <code>object</code></a>:</p> <pre><code>class Swoosh(object): </code></pre> <p>Defining an <code>__init__</code> method that takes arguments is definitely the Pythonic way of doing things:</p> <pre><code>def __init__(self,spam,eggs,swallow,coconut): self.spam = spam self.eggs = eggs self.swallow = swallow self.coconut = coconut </code></pre> <p>This would allow you to do:</p> <pre><code>s = Swoosh('Spam!','Eggs','Swallow','Migrated') </code></pre> <p>Like any other Python function the <code>__init__</code> method can have default values for arguments, e.g.</p> <pre><code>def __init__(self,spam,eggs,swallow,coconut='Migrated.'): self.spam = spam self.eggs = eggs self.swallow = swallow self.coconut = coconut </code></pre> <p>If you want to validate attributes as they're set you should use standard <a href="http://docs.python.org/library/functions.html#property"><code>property</code> attributes</a> rather than creating your own setters. If you do it this way you can use <code>myobject.spam</code> in your code like with an ordinary attribute but your setter method is still run.</p> <p>For example:</p> <pre><code>@property def spam(self): """I'm the 'spam' property.""" return self._spam @spam.setter def spam(self, value): if not value.endswith("!"): raise ValueError("spam must end with !") # Store the value in "private" _spam attribute self._spam = value @spam.deleter def spam(self): del self._spam </code></pre> <p>Note: <code>property</code> will <strong>not work</strong> unless you're using new-style classes as described above.</p> <p>In your example you have:</p> <pre><code>class Swoosh(): spam = '' eggs = '' swallow = '' coconut = '' </code></pre> <p>This is often presented as a quick way of setting defaults for attributes for an object. It's fine but you need to understand what is actually happening. What's going on is that you're setting attributes on the <em>class</em>. If an object doesn't have an attribute Python will look to see if it's defined on the class and return the value from there (as this is what you want for methods).</p> <p>If you want to set default values for object attributes you're much better off setting default values for the arguments in <code>__init__</code> as described above rather than using <code>class</code> attributes.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload