Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In order:</p> <ol> <li>using this form is simply syntactic sugar for calling the list constructor - i.e. you are creating a new (empty) list. This will be bound to the class itself (is a static field) and will be the same for all instances.</li> <li>apart from the constructor name which must always be <em><strong>init</strong></em>, both are valid forms, but mean different things. <ul> <li>The first constructor can be called with a list as argument or without. If it is called without arguments, the empty list passed as default is used within (this empty list is created once during class definition, and not once per constructor call), so no items are added to the static list. </li> <li>The second <strong>must</strong> be called with a list parameter, or python will complain with an error, but using it without the <em>self.</em> prefix like you are doing, it would just create a new local variable name <em>List</em>, accessible only within the constructor, and leave the static <em>A.List</em> variable unchanged.</li> </ul></li> <li>Deleting will only unlink a reference to the object, without actually deleting anything. Once all references are removed, however, the garbage collector is free to clear the memory as needed. It is usually a bad idea to try to control the garbage collector. instead. just make sure you don't hold references to objects you no longer need and let it make its work.</li> <li>Assigning a variable with an object will only create a new reference to the same object, yes. To create a deep copy use the <a href="http://docs.python.org/2/library/copy.html" rel="nofollow">related functions</a> or write your own.</li> <li>Operator overloading (use with care, it can make things more confusing instead of clearer if misused) can be done by overriding some <a href="http://docs.python.org/2/reference/datamodel.html#special-method-names" rel="nofollow">special methods</a> in the class definition.</li> </ol> <p>About your edit: like i pointed above, when writing <em>List=list()</em> inside the constructor, without the <em>self.</em> (or better, since the variable is static, <em>A.</em>) prefix, you are just creating an empty variable, and <strong>not</strong> overriding the one you defined in the class body.</p> <p>For reference, the usual way to handle a list as default argument is by using a <em>None</em> placeholder:</p> <pre><code>class A(object): def __init__(self, arg=None): self.startvalue = list(arg) if arg is not None else list() # making a defensive copy of arg to keep the original intact </code></pre> <p>As an aside, do take a look at the python tutorial. It is <strong>very</strong> well written and easy to follow and understand.</p>
 

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