Posts: 7
Threads: 3
Joined: Aug 2019
Can I create alias variables for parts of another variable ? For example, I have variables as below.
full_date = '20190827'
yyyy = full_date[0:4] ### Want this as mutable
mm = full_date[4:6] ### Want this as mutable
dd = full_date[6:8] ### Want this as mutable
# Now, if I do
mm -= 1
# I expect full_date = 20190727
# I do not want to use full_date[x:y] every time
Is the above possible ?
Thanks in advance for the help!
Posts: 8,169
Threads: 160
Joined: Sep 2016
Aug-27-2019, 07:01 AM
(This post was last modified: Aug-27-2019, 07:01 AM by buran.)
strings are immutable, so the above example is not possible.
If variable is of mutable type, like list or dict it's possible to do
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
>>> spam = [ 1 , 2 , 3 ]
>>> id (spam)
30827144
>>> spam[ 0 ] + = 1
>>> spam
[ 2 , 2 , 3 ]
>>> id (spam)
30827144
>>> eggs = { 'foo' : 1 , 'bar' : 2 }
>>> id (eggs)
31299768
>>> eggs[ 'bar' ] + = 1
>>> eggs
{ 'foo' : 1 , 'bar' : 3 }
>>> id (eggs)
31299768
>>>
|
as you can see from the ids it's the same list/dict.
because your example is for date, you can use datetime module
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
>>> from datetime import datetime
>>> spam = datetime.strptime( '20190827' , '%Y%m%d' )
>>> spam
datetime.datetime( 2019 , 8 , 27 , 0 , 0 )
>>> spam.replace(month = 9 )
datetime.datetime( 2019 , 9 , 27 , 0 , 0 )
>>> spam = datetime.strptime( '20190831' , '%Y%m%d' )
>>> spam.replace(month = 9 )
Traceback (most recent call last):
File "<stdin>" , line 1 , in <module>
ValueError: day is out of range for month
>>> spam.strftime( '%Y-%m-%d' )
'2019-08-31'
>>>
|
Posts: 7
Threads: 3
Joined: Aug 2019
Thanks for the response Buran !
I'm not particular about date, I just gave an example.
From your first example, can I have separate names for spam[0], spam[1], spam[2] ? For example,
spam = [1, 2, 3]
a1 = spam[0]
a2 = spam[1]
a3 = spam[2]
a2 += 1 #Expecting spam = [1, 3, 3]
Is this possible ?
Posts: 7,326
Threads: 123
Joined: Sep 2016
(Aug-28-2019, 02:09 PM)mln4python Wrote: From your first example, can I have separate names for spam[0], spam[1], spam[2] ? Yes as you do or unpack,a1,a2,a3 is now separate element with no contention to spam list.
So if make change to a2 need to update list if want to use this new value.
1 2 3 4 5 6 7 8 9 10 |
>>> spam = [ 1 , 2 , 3 ]
>>> a2 = spam[ 1 ]
>>> a2
2
>>> a2 + = 1
>>> a2
3
>>> spam[ 1 ] = a2
>>> spam
[ 1 , 3 , 3 ]
|
An other way modify list first then unpack of course this all depend on task to solve.
1 2 3 4 5 6 7 8 9 10 11 |
>>> spam = [ 1 , 2 , 3 ]
>>> spam[ 1 ] + = 1
>>> spam
[ 1 , 3 , 3 ]
>>> a1, a2 ,a3 = spam
>>> a1
1
>>> a2
3
>>> a3
3
|
If not sure if it's separate element or there are some connection use id() as shown.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
>>> spam = [ 1 , 2 , 3 ]
>>> a2 = spam[ 1 ]
>>> id (spam)
6359520
>>> id (a2)
255747184
>>>
>>> help ( id )
Help on built - in function id in module builtins:
id (obj, / )
Return the identity of an object .
This is guaranteed to be unique among simultaneously existing objects.
(CPython uses the object 's memory address.)
|
|