Use 'Self' as a Default Parameter

Using self.xxxx as a default parameter - Python

larsmans answered your first question

For your second question, can you simply look before you leap to avoid recursion?

def makeList(self, aNode=None):
if aNode is None:
aNode = self.root
treeaslist = [aNode.data]
if aNode.lChild:
treeaslist.extend(self.makeList(aNode.lChild))
if aNode.rChild:
treeaslist.extend(self.makeList(aNode.rChild))
return treeaslist

Use `self` as a default parameter


Can I use self as a default parameter in Swift

No. This is basically for the same reason that you can't say self when initializing an instance method:

class TreeNode {
var otherNode : TreeNode = self // Use of unresolved identifier `self`
func printTree(tree:TreeNode = self) { } // Use of unresolved identifier `self`
}

At the time you're defining this entity (property or method), there is no such thing as self. There will be a self later, when this method is called, inside the body of the function - because the function will be called on some instance, namely self.

One easy way to do what you want to do is to make this parameter an Optional with a nil default value. Then, in the body of the function, check for nil and use self:

class TreeNode {
func printTree(tree:TreeNode? = nil) {
let treeToPrint = tree ?? self
// do stuff
}
}

Now it's legal to call printTree with no parameter, and it does what you want it to do:

TreeNode().printTree()

Is it possible to use self.value as default argument in method?

I don't believe you can use an instance attribute value as a default. You can work around it by defaulting to None, then doing the rest inside the function:

def print_element(self, element=None):
if element is None:
element = self.value

Using self.* as default value for a method

Use a default of None and detect that.

def save_file(self, outputfilename=None): 
if outputfilename is None:
outputfilename = self.image_filename
self.file.read(outputfilename)
....

Passing self.var (instance attribute) as default method parameter

Method (and function) default parameter values are resolved when the method is defined. This leads to a common Python gotcha when those values are mutable: "Least Astonishment" and the Mutable Default Argument

In your case, there is no self available when the method is defined (and if there was such a name in scope, as you haven't actually finished defining the class Foo yet, it wouldn't be a Foo instance!) You can't refer to the class by name inside the definition either; referring to Foo would also cause a NameError: Can I use a class attribute as a default value for an instance method?

Instead, the common approach is to use None as a placeholder, then assign the default value inside the method body:

def fiz(self, num1=None):
if num1 is None:
num1 = self.val1
...

Evaluating self inside function

Function default values are evaluated when the function is defined, not every time the function is called without the necessary argument. As such, self is just a name, not the object invoking the function.

Instead, you just need a sentinel, which is a value that you can use at runtime to determine if an argument was passed. Typically, you can use None, though when None is a valid argument, you'll need to choose a different value.

def func(self, test=None):
if test is None:
test = self.text
return test


Related Topics



Leave a reply



Submit