Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

array of objects in python

class TestDat(object):
     Dat1 = None
     Dat2 = None
#Declaring the Test Array
TestArray = []
#Declaring the object
Test1 = TestDat()
#Defining the member variables in said object
Test1.Dat1 = 0
Test1.Dat1 = 1
#Appending the object to the List
TestArray.append(Test1)
#Rewriting and appending again
Test1.Dat1 = 3
Test1.Dat1 = 4
TestArray.append(Test1)
#Printing our Our Results
print TestArray[0].Dat1
print TestArray[1].Dat1
Comment

python array of objects

class TestDat():          # leave this empty
    def __init__(self):   # constructor function using self
        self.Dat1 = None  # variable using self.
        self.Dat2 = None  # variable using self
    
TestArray = [] #empty array

Test1 = TestDat() #this is an object
Test2 = TestDat() #this is another object
        
Test1.Dat1 = 0 #assigning value to object 1 
Test1.Dat2 = 1 #assigning value to object 1 
    
Test2.Dat1 = 3 #assigning value to object 2 
Test2.Dat2 = 4 #assigning value to object 2

TestArray.append(Test1) #append object 1
TestArray.append(Test2) #append object 2 
    
print (TestArray[0].Dat1) # this is Test1
print (TestArray[1].Dat1) # this is Test2

# or even simpler:

class TestDat():
    def __init__(self, Dat1, Dat2):
        self.Dat1 = Dat1
        self.Dat2 = Dat2

TestArray = [TestDat(0,1),
             TestDat(3,4)]

print (TestArray[0].Dat1) # this is Test1
print (TestArray[1].Dat1) # this is Test2

# or this way:

class TestDat():
    def __init__(self):
        self.Dat1 = None
        self.Dat2 = None
    
TestArray = [] #empty array
size = 2       #number of loops

for x in range(size):  # appending empty objects
    TestArray.append(TestDat())

#initialize later
TestArray[0].Dat1 = 0
TestArray[0].Dat2 = 1

TestArray[1].Dat1 = 3
TestArray[1].Dat2 = 4

print("print everithing")
for x in range(len(TestArray)):
    print("object "+str(x))
    print(TestArray[x].Dat1)
    print(TestArray[x].Dat2)
Comment

PREVIOUS NEXT
Code Example
Python :: cmap seaborn 
Python :: how to make python faster than c++ 
Python :: how to use str() 
Python :: python print array 
Python :: activate virtual environment python in linux 
Python :: python transpose 
Python :: pandas idxmax 
Python :: master python 
Python :: Python String count() example 
Python :: dot product of two vectors python 
Python :: pycryptodome rsa encrypt 
Python :: string template python 
Python :: how to create multiple columns after applying a function in pandas column python 
Python :: python cast to int 
Python :: simulation? 
Python :: signup 
Python :: .pop python 
Python :: web3.py failing not installing 
Python :: asyncioevents.py", line 504, in add_reader raise NotImplementedError 
Python :: expected a list of items but got type int . django 
Python :: how to set default value in many2one 
Python :: shutdown thread python 
Python :: comparing dict key with integer 
Python :: how to send message to specific channel discord/py 
Python :: spearman correlation seaborn 
Python :: python with statement variables 
Python :: What is StringIndexer , VectorIndexer, and how to use them? 
Python :: "DO_NOTHING" is not defined django 
Python :: py ocmpare strings 
Python :: type operator in python 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =