4

Can I use self parameters in my method definition in python?

class Test: def __init__(self, path): self.path = pathlib.Path(path) def lol(self, destination=self.path): x = do_stuff(destination) return x 

I can make def lol(self, destination) and use it this way test_obj.lol(test_obj.path). But is there a way to set default destination arg to self.path? The other way posted below(based on this answers), but can I refactor it somehow and make it more elegant? Maybe there is new solution in python3.+ versions.

def lol(self, destination=None): if destination in None: destination = self.path x = do_stuff(destination) return x 
10
  • 1
    It's possible to dynamically add an instance method inside the class' __init__. There you have access to the instance variables. Commented Oct 17, 2018 at 11:29
  • @a_guest, but can I use method defined in __init__ from outside? Commented Oct 17, 2018 at 11:32
  • def lol(self, destination=self.path): this declaration already has the default you are looking for. Just pass whatever you want to, to destination if you want it to take any other value. Commented Oct 17, 2018 at 11:33
  • 1
    @Alex In addition you need to set it as an instance attribute via self.lol = lol. Then you can access it normally. I'll post an example later. Commented Oct 17, 2018 at 11:34
  • 1
    The parameter is evaluated when the function is defined, not when it is called or when the object is created. There is no object called "self" when the function is defined, unless you do something weird like have a global variable of a different type that also has a path property. Commented Oct 17, 2018 at 11:49

2 Answers 2

2

No. This leads to problems, since self.Path will probably change over runtime. But the default arguments are all evaluated at creation time. Therefore destination will always be a static value which might be different to self.Path at any point.

EDIT:

see Why are default arguments evaluated at definition time in Python?

Sign up to request clarification or add additional context in comments.

Comments

2

How I'd refactor the code:

def func_test(self, destination=None): return do_stuff(destination or self.path) 

The above is the cleanest and riskiest. - a best case scenario where you know the value, and that or fits in perfectly. - otherwise careless to use it.

Otherwise I would opt for:

def func_test(self, destination=None): return do_stuff(destination if destination is not None else self.path) 

In regards to passing self.property to a function argument; simply no.

4 Comments

This does not explicitly check for None. It will fallback at any false value. This is definitely different than the OP. In addition it is not answering the question (which is about instance variables as default arguments for instance methods).
Don't use or carelessly. What if I pass destination=''?
So this will work if destination != False/None/empty_value, right?
@Alex or as noted by others, is quite careless, unless you're absolutely sure about the data type coming in. - or will fallback at any false value (meaning '', False, None etc.) - if that's what you need, this is perfect for you. - But as @Aran-Fey said, be attentive of where you use it. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.