Custom variable type?

This is the place for queries that don't fit in any of the other categories.

Custom variable type?

Postby eslavko » Sat Dec 28, 2013 6:30 pm

Hello...
As newbie in python I got stuck in custom variable type... Maybe my expression is wrong.
At glance I'd like to have list of elements like
list[0].name
list[0].age
list[1].name
list[1].age
....
But I stuck. In code below I got same result every time.

Code: Select all
class rungCell(object):
    name = None
    def __init__(self, name):
        self.name = name
    age = None
    def __init__(self, age):
        self.age = age


mlist=[]
mlist.append(rungCell)
mlist.append(rungCell)

print mlist[0].name
print mlist[1].name

mlist[0].name="NAME 1"
mlist[1].name="NAME 2"

print mlist[0].name
print mlist[1].name
eslavko
 
Posts: 25
Joined: Thu Dec 26, 2013 10:23 am

Re: Custom variable type?

Postby Yoriz » Sat Dec 28, 2013 6:46 pm

Code: Select all
class RungCell(object):

    def __init__(self, name=None, age=None):
        self.name = name
        self.age = age


mlist = []
mlist.append(RungCell())
mlist.append(RungCell())

print mlist[0].name
print mlist[1].name

mlist[0].name = "NAME 1"
mlist[1].name = "NAME 2"

print mlist[0].name
print mlist[1].name
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1164
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Coding Help

Who is online

Users browsing this forum: stranac and 3 guests