On 20/05/2023 18:54, Alex Jando wrote:
I have many times had situations where I had a variable of a certain type, all
I cared about it was one of it's methods.
For example:
------------------------------------------------------------
import hashlib
hash = hashlib.sha256(b'word')
hash = hash.hexdigest()
------------------------------------------------------------
import enum
class Number(enum.Enum):
One: int = 1
Two: int = 2
Three: int = 3
num = Number.One
num = num.value
------------------------------------------------------------
Now to be fair, in the two situations above, I could just access the method
right as I declare the object, however, sometimes when passing values into
functions, it's a lot messier to do that.
So what I'm suggesting is something like this:
------------------------------------------------------------
import hashlib
hash = hashlib.sha256(b'word')
hash.=hexdigest()
------------------------------------------------------------
import enum
class Number(enum.Enum):
One: int = 1
Two: int = 2
Three: int = 3
num = Number.One
num.=value
------------------------------------------------------------
It seems to me that this would encourage bad style. When you write
num = num.value
you are using num with two different meanings (an object and an
attribute of it). The original object is lost.
If down the line you needed to access another attribute of the object,
you would have to backtrack:
val = num.value
spam = num.spam
or at least write the statements in the right order, so that you only
overwrite num at the end:
spam = num.spam
num = num.value
Either way, you are making the code more fragile i.e. harder to maintain.
Now of course, in your use case it may be perfectly appropriate to write
"num = num.value" as you did.
But IMO it's not something that should be encouraged in general.
Best wishes
Rob Cliffe
--
https://mail.python.org/mailman/listinfo/python-list