Question Details

No question body available.

Tags

python arguments

Answers (2)

November 2, 2025 Score: 1 Rep: 11 Quality: Low Completeness: 60%

your core issue is that your function's default arguments (title='NewTitle') are indistinguishable from arguments explicitly passed by the user.

The idiomatic Python solution is to use "None" as the default argument value. This acts as a signal to mean "this argument was not provided." You can then test for "None inside the function to decide whether to apply an update or let the class constructor handle its own defaults.

Here is the code you should use. :
class Feature:
    def init(self,
            title='DefaultTitle',
            number=-1):
        self.title=title
        self.number=number

def repr(self): return f"Feature(title='{self.title}', number={self.number})"

def addOrUpdateFeature( existingFeature=None, title=None, number=None):

if existingFeature: if title is not None: existingFeature.title = title if number is not None: existingFeature.number = number return True

else: kwargsforcreate = {} if title is not None: kwargsforcreate['title'] = title if number is not None: kwargsforcreate['number'] = number

return Feature(kwargsforcreate)

a = addOrUpdateFeature(title='featureA', number=1) print(f'part 1: {a}')

addOrUpdateFeature(existingFeature=a, number=2) print(f'part 2: {a}')

b = addOrUpdateFeature() print(f'part 3: {b}')
November 2, 2025 Score: 0 Rep: 1,191 Quality: Low Completeness: 80%

Your code has a few minor issues.

  1. When you call a method, you only pass the arguments that it needs to use:

    myMethod( number = 1 )

doesn't make much sense (this is used (as you well know) in the method declaration, when it needs default parameters), if you need to keep the original variable (this is not the case, since it is implicit in the object), use:

number = 1
myMethod( number )

and if you don't need it:

myMethod( 1 )
  1. Your 2addOrUpdateFeature method returns a Boolean when existingFeature is not None, and a Feature otherwise, which causes some problems during execution.

class Feature:
    def init( self, title='DefaultTitle', number=-1 ):
        self.title=title
        self.number=number

def addOrUpdateFeature( existingFeature=None, title='NewTitle', number=0 ): if existingFeature:

# only modify "number" existingFeature.number = number

# we need return the object, but if return "true", # the "print" method... "caput" return existingFeature else:

# return new instance return Feature( title, number)

# we instantiate the object by passing only the parameters. a = Feature( 'featureA', 114 )

# we pass the created object as the first parameter so that # it modifies its number, we pass an empty string as the # second parameter, since it will be ignored. a = addOrUpdateFeature( a, "", 1 )

print( f'part 1: title={a.title} number={a.number}' )

# Here we assign a new object to “a” since we pass None # as the first parameter, so the code in the else clause # will be executed. a = addOrUpdateFeature( None, 'featureB', 2 )

print(f'part 2: title={a.title} number={a.number}')class Feature: