Is there any stylistic taboo or other downside to implementing trivial methods by assignment to class attributes?  E.g. like bar and baz below, as opposed to the more ususal foo.
class MyClass(object):
    def hello(self):
        return 'hello'
    def foo(self):
        return self.hello()
    bar = lambda self: self.hello()
    baz = hello
I find myself tempted by the apparent economy of things like this:
__str__ = __repr__ = hello
                A method could also utilize the attributes that you defined within the object itself. Another key difference between a method and attribute is how you call it. For example, let's say we create an instance using the above class we defined. Save this answer.
Attributes of a class are function objects that define corresponding methods of its instances. They are used to implement access controls of the classes. Attributes of a class can also be accessed using the following built-in methods and functions : getattr() – This function is used to access the attribute of object.
Python setattr() method is used to assign the object attribute its value.
getattr() − A python method used to access the attribute of a class.
Personally, I think things like
__str__ = __repr__ = hello
are fine, but
bar = lambda self: self.hello()
is evil.  You cannot easily give a lambda a docstring, and the .func_name attribute will have the meaningless value <lambda>.  Both those problems don't occur for the first line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With