Skip to main content
added 181 characters in body
Source Link
Alex
  • 1.4k
  • 2
  • 26
  • 45

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 

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, but can I refactor it somehow?

def lol(self, destination=None): if destination in None: destination = self.path x = do_stuff(destination) return x 

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 
Post Closed as "Duplicate" by Aran-Fey python
Source Link
Alex
  • 1.4k
  • 2
  • 26
  • 45

Use class self variable as default class method argument

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, but can I refactor it somehow?

def lol(self, destination=None): if destination in None: destination = self.path x = do_stuff(destination) return x