3 ms·
Yes, you can replace attributes with properties without changing the consuming code. For example: >>> class Foo: ... @property ... def bar(
by Nullabillity 4y ago
Yes, you can replace attributes with properties without changing the consuming code. For example:
>>> class Foo:
... @property
... def bar(self):
... return "hello from a property"
... @bar.setter
... def bar(self, value):
... print(f"tried to replace bar with {value}")
>>> Foo().bar
'hello from a property'
>>> Foo().bar = "asdf"
tried to replace bar with asdf
- chrisseaton 4y agoI know it's completely subjective and intellectually irrelevant, but wow I think those annotations are super ugly. In Ruby it's all just method calls - properties, methods, user control-flow, everything.
- jefftk 4y agoIt's all just method calls in python too! @foo def bar(): ... Is just syntactic sugar for: def bar(): ... bar = foo(bar)
- remram 4y agoIt's a bit of a weird take when attribute definition in Ruby also uses at signs specifically. Which part do you find ugly?
- chrisseaton 4y ago> It's a bit of a weird take when attribute definition in Ruby also uses at signs specifically. No that’s instance variables. > Which part do you find ugly? In Ruby to modify a method you just call a method. I guess I think a second, redundant, way to call methods is not elegant.
- rat87 4y agoIn Ruby if else is not a method for some reason also blocks are not objects for some reason "seems inelegant"
- chrisseaton 4y ago> In Ruby if else is not a method for some reason This is because there’s not a good way to pass two delayed computations - two blocks. Looping constructs were replaced by method variants. > also blocks are not objects for some reason Yeah they’re reifiable but not sure why not reified by default.
- rat87 4y agoBut passing two blocks/lambdas/etc doesn't inherently need to be that difficult it's not in other languages. I don't know that much about Ruby's implementation but I think both the if else and blocks not being objects are that way to prevent Ruby from being even slower. My understanding is if you turn a block into a proc it gets a lot slower.
- remram 4y agoInstance variables != attributes? If that's the case what do you call "attribute"?
- chrisseaton 4y agoAttributes are an interface - a getter and setter method with predictable names. They can be implemented with instance variables as the underlying storage, but they don't have to be.