Python Forum
Alias for different parts of a variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Alias for different parts of a variable
#1
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!
Reply
#2
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
>>> 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

>>> 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'
>>>
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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 ?
Reply
#4
(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.
>>> 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.
>>> 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.
>>> 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.)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Alias names dukbilt 1 2,344 Jan-22-2019, 05:29 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020