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
------------------------------------------------------------
On 21/05/2023 05.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
------------------------------------------------------------
A custom-class wrapper?
Even, a decorator-able function?
On 21/05/2023 05.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
------------------------------------------------------------
A custom-class wrapper?
Even, a decorator-able function?
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:
------------------------------------------------------------
hash = hash.hexdigest() ------------------------------------------------------------
num = num.value
------------------------------------------------------------
So what I'm suggesting is something like this:
------------------------------------------------------------ hash.=hexdigest() ------------------------------------------------------------
num.=value
------------------------------------------------------------
On 2023-05-20 10:54:59 -0700, Alex Jando wrote:
I have many times had situations where I had a variable of a certainI actually needed to read those twice to get their meaning. I think
type, all I cared about it was one of it's methods.
For example:
------------------------------------------------------------
hash = hash.hexdigest()
------------------------------------------------------------
num = num.value
------------------------------------------------------------
So what I'm suggesting is something like this:
------------------------------------------------------------
hash.=hexdigest()
------------------------------------------------------------
num.=value
------------------------------------------------------------
hash .= hexdigest()
num .= value
would have been clearer (yes, I nag my colleagues about white-space,
too).
Do you have any examples (preferably from real code) where you don't
assign to a simple variable? I feel that
x += 1
isn't much of an improvement over
x = x + 1
but
self.data[line+len(chars)-1] += after
is definitely an improvement over
self.data[line+len(chars)-1] + self.data[line+len(chars)-1] + after
hp
hash.=hexdigest()
This just can't happen (as far as I can figure) for .= unless the object
is defining something weird for the inplace version of the operation,
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.It seems to me that this would encourage bad style. When you write
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
------------------------------------------------------------
On 20/05/2023 18:54, Alex Jando wrote:
So what I'm suggesting is something like this:
------------------------------------------------------------It seems to me that this would encourage bad style. When you write
hash = hashlib.sha256(b'word')
hash.=hexdigest() ------------------------------------------------------------
num = Number.One
num.=value
------------------------------------------------------------
num = num.value
you are using num with two different meanings (an object and an
attribute of it).
But I find it easier to read if I just reuse the same variable name:
user = request.GET["user"]
user = str(user, encoding="utf-8")
user = user.strip()
user = user.lower()
user = orm.user.get(name=user)
Each instance only has a livetime of a single line (or maybe two or
three lines if I have to combine variables), so there's little risk of confusion, and reusing the variable name makes it very clear that all
those intermediate results are gone and won't be used again.
This sort of code might be better as a single expression. For example:
user = (
request.GET["user"]
.decode("utf-8")
.strip()
.lower()
)
user = orm.user.get(name=user)
This sort of code might be better as a single expression. For example:
user = (
request.GET["user"]
.decode("utf-8")
.strip()
.lower()
)
user = orm.user.get(name=user)
LOL. And I thought I was the one with a (self-confessed) tendency to
write too slick, dense, smart-alec code. 😁
Indeed, I was itching to shorten it (starting with the low-hanging
fruit: user = user.strip().lower() ).
Seriously though: this kind of condensation can come unstuck when any of
the steps need to be made more complicated.
(Suppose request.GET might return ASCII, might return Unicode, depending
on which server it was talking to.)
On 2023-05-24 07:12:32 +1000, Chris Angelico wrote:
On Wed, 24 May 2023 at 07:04, Peter J. Holzer <hjp-python@hjp.at> wrote:
But I find it easier to read if I just reuse the same variable name:
user = request.GET["user"]
user = str(user, encoding="utf-8")
user = user.strip()
user = user.lower()
user = orm.user.get(name=user)
Each instance only has a livetime of a single line (or maybe two or
three lines if I have to combine variables), so there's little risk of confusion, and reusing the variable name makes it very clear that all those intermediate results are gone and won't be used again.
Small side point: You can make use of the bytes object's decode()
method to make the chaining much more useful here, rather than the
str() constructor.
This sort of code might be better as a single expression. For example:
user = (
request.GET["user"]
.decode("utf-8")
.strip()
.lower()
)
user = orm.user.get(name=user)
Yes, that probably wasn't the best example. I sort of deliberately
avoided method chaining here to make my point that you don't have to
invent a new variable name for every intermediate result, but of course
that backfired because in this case you don't need a variable name at
all. I should have used regular function calls ...
On Wed, 24 May 2023 at 07:04, Peter J. Holzer <hjp-python@hjp.at> wrote:
But I find it easier to read if I just reuse the same variable name:
user = request.GET["user"]
user = str(user, encoding="utf-8")
user = user.strip()
user = user.lower()
user = orm.user.get(name=user)
Each instance only has a livetime of a single line (or maybe two or
three lines if I have to combine variables), so there's little risk of confusion, and reusing the variable name makes it very clear that all
those intermediate results are gone and won't be used again.
Small side point: You can make use of the bytes object's decode()
method to make the chaining much more useful here, rather than the
str() constructor.
This sort of code might be better as a single expression. For example:
user = (
request.GET["user"]
.decode("utf-8")
.strip()
.lower()
)
user = orm.user.get(name=user)
Do you mean "ASCII or UTF-8"? Because decoding as UTF-8 is fine with
ASCII (it's a superset). You should always consistently get the same
data type (bytes or text) based on the library you're using.
ChrisAOK, bad example. The point remains: condensing each step to a single
chained function can come unstuck when one of the steps needs to be made
more complicated for some reason.
On 2023-05-21 20:30:45 +0100, Rob Cliffe via Python-list wrote:
On 20/05/2023 18:54, Alex Jando wrote:I think that's ok if it's the same thing at a high level.
So what I'm suggesting is something like this:It seems to me that this would encourage bad style. When you write
------------------------------------------------------------
hash = hashlib.sha256(b'word')
hash.=hexdigest()
------------------------------------------------------------
num = Number.One
num.=value
------------------------------------------------------------
num = num.value
you are using num with two different meanings (an object and an
attribute of it).
I sometimes have a chain of transformations (e.g. first decode it, then
strip extra spaces, then normalize spelling, then look it up in a
database and replace it with the record, ...). Technically, of course
all these intermediate objects are different, and I could make that
explicit by using different variable names:
user_param = request.GET["user"]
user_decoded = str(user_param, encoding="utf-8")
user_stripped = user_decoded.strip()
user_normalized = user_stripped.lower()
user_object = orm.user.get(name=user_normalized)
But I find it easier to read if I just reuse the same variable name:
user = request.GET["user"]
user = str(user, encoding="utf-8")
user = user.strip()
user = user.lower()
user = orm.user.get(name=user)
Each instance only has a livetime of a single line (or maybe two or
three lines if I have to combine variables), so there's little risk of confusion, and reusing the variable name makes it very clear that all
those intermediate results are gone and won't be used again.
hp
On Wed, 24 May 2023 at 08:48, Peter J. Holzer <hjp-python@hjp.at> wrote:
Yes, that probably wasn't the best example. I sort of deliberately
avoided method chaining here to make my point that you don't have to
invent a new variable name for every intermediate result, but of course that backfired because in this case you don't need a variable name at
all. I should have used regular function calls ...
In the context of a .= operator, though, that is *in itself* an
interesting data point: in order to find an example wherein the .=
operator would be plausible, you had to make the .= operator
unnecessary.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 06:18:41 |
Calls: | 10,386 |
Calls today: | 1 |
Files: | 14,058 |
Messages: | 6,416,634 |