Python新手面向对象编程疑问:如何实现ObjectA.add.func1()结构
Hey there! As a fellow Python dev who’s been down the OOP learning curve, let’s figure out how to make that ObjectA.add.func1() syntax work exactly how you want.
Solution: Use a Nested Helper Class for the
add Interface First, let's break down what you need:
- An
ObjectAinstance that stores functions via anaddinterface (with that cleanadd.func1()syntax) - An
apply()method that runs all stored functions on a float vector
Here's a straightforward, beginner-friendly implementation that hits all your requirements:
class ObjectA: def __init__(self): # Internal list to hold our functions self._registered_functions = [] # Initialize the `add` interface as an instance of our nested helper class self.add = self._FunctionAdder(self) def apply(self, vector): # Validate the input is a float-only vector if not all(isinstance(val, float) for val in vector): raise ValueError("Vector must contain only floating-point numbers") # Apply each function in sequence current_vector = vector.copy() for func in self._registered_functions: current_vector = func(current_vector) return current_vector # Nested helper class to handle the `add.funcX()` syntax class _FunctionAdder: def __init__(self, parent_instance): # Keep a reference to the parent ObjectA instance self.parent = parent_instance def __getattr__(self, function_name): # Try to fetch the function from the global scope import __main__ try: target_func = getattr(__main__, function_name) except AttributeError: raise AttributeError(f"Function '{function_name}' doesn't exist in your scope!") # Return a wrapper that registers the function when called def register_function(): if callable(target_func): self.parent._registered_functions.append(target_func) print(f"Registered function: {function_name}") else: raise ValueError(f"'{function_name}' isn't a callable function!") return register_function # ------------------------------ # Example Usage # ------------------------------ def func1(vec): # Add 1 to every element in the vector return [x + 1.0 for x in vec] def func2(vec): # Multiply every element by 2 return [x * 2.0 for x in vec] # Create our ObjectA instance my_obj = ObjectA() # Register functions using your desired syntax my_obj.add.func1() my_obj.add.func2() # Test with a float vector test_vec = [1.5, 2.5, 3.5] result = my_obj.apply(test_vec) print(result) # Output: [5.0, 7.0, 9.0] (1.5+1)*2=5, etc.
How This Works:
- Nested
_FunctionAdderClass: This acts as theaddinterface. It holds a reference to the parentObjectAinstance, so it can modify the parent's list of registered functions without cluttering the main class. __getattr__Magic Method: When you callmy_obj.add.func1(), Python can't find afunc1attribute on the_FunctionAdderinstance, so it triggers__getattr__. This method grabs thefunc1function from your global scope and returns a wrapper.- Wrapper Function: When you call
func1()(viaadd.func1()), the wrapper adds the actualfunc1to the parent's_registered_functionslist. apply()Method: Validates your input vector is float-only, then runs every registered function in the order they were added.
Bonus Tips for Beginners:
- Private Attributes: We used
_registered_functions(with a leading underscore) to signal it's an internal attribute—avoid modifying it directly from outside the class! - Extendability: You could add extra methods to
_FunctionAdder, likelist()to show all registered functions, orremove(func_name)to delete a function. - Alternative for Explicit Registration: If you want to avoid relying on global scope, you could modify
_FunctionAdderto accept functions directly, likemy_obj.add.register(func1)—but your requestedadd.func1()syntax is covered perfectly above.
内容的提问来源于stack exchange,提问作者hashka92
相关产品推荐
相关产品推荐

