class wise object detection product count in tensorflow 2 - python

Can we count detected objects in Tensorflow 2 object detection API class wise?
as i am new to this so having hard time in manipulating the output of object detection model according to my use case described below
Say you have two classes tomato and potato in a super market shelf stock, I would like to count each object class wise
for example; potato_count:5 tomato_count:3.
for reference the following output i have is like this, just to give an idea:-
{'raw_detection_scores': array([[3.8237274e-03, 3.1729043e-03, 5.1983595e-03, ..., 1.0126382e-02,4.1468740e-03, 3.5721064e-03],
[3.7932396e-03, 1.9723773e-03, 2.3661852e-03, ..., 3.4036636e-03,
9.3266368e-04, 4.3996871e-03],
[3.2063425e-03, 2.9956400e-03, 3.9784312e-03, ..., 5.5939257e-03,
2.3936033e-03, 2.7040839e-03],
...,
[4.1239262e-03, 3.9246678e-04, 4.5044391e-05, ..., 1.2922287e-04,
2.8958917e-04, 2.2355914e-03],
[1.4656782e-03, 4.8859119e-03, 1.4899671e-03, ..., 2.8479993e-03,
2.8250813e-03, 2.1298528e-03],
[1.8135607e-03, 2.2478402e-03, 1.1820495e-03, ..., 9.5197558e-04,
1.3802052e-03, 2.2761822e-03]], dtype=float32), 'detection_anchor_indices': array([44692., 44710., 44728., 44818., 39652., 44674., 39670., 40036.,
40018., 44800., 39634., 39371., 44830., 38090., 44728., 44731.,
44728., 44710., 10078., 39796., 27838., 37604., 16933., 24833.,
39778., 44659., 45058., 38084., 44791., 44710., 44692., 30244.,
5284., 38090., 37604., 38204., 33593., 38192., 37982., 39796.,
44692., 6635., 33118., 24389., 37604., 44910., 33112., 39601.,
16133., 3845., 39918., 48370., 19204., 44740., 39778., 16792.,
6629., 25763., 38150., 48187., 15839., 38180., 23524., 44914.,
40036., 1438., 25763., 38078., 35992., 38012., 39888., 38084.,
44578., 40018., 51075., 38204., 15833., 37976., 40258., 37604.,
48751., 39906., 31684., 16453., 38054., 5140., 42568., 36484.,
38012., 38202., 37946., 14024., 2404., 40002., 5764., 39870.,
48823., 26878., 38198., 39430.], dtype=float32), 'detection_multiclass_scores': array([[1.6726255e-03, 3.2217724e-07, 2.8865278e-02, ..., 2.6032329e-04,
2.0583175e-05, 4.5886636e-04],
[1.0796189e-03, 5.8811463e-07, 3.6984652e-02, ..., 6.6033006e-04,
3.2279208e-05, 4.3705106e-04],
[6.1860681e-04, 2.4921978e-06, 6.0835034e-02, ..., 2.0631850e-03,
5.2474130e-05, 3.7664175e-04],
...,
[2.0163953e-03, 1.0121465e-03, 1.6601086e-03, ..., 5.1327944e-03,
1.6998947e-03, 9.6607208e-04],
[1.2855232e-03, 5.4006279e-03, 1.0573506e-02, ..., 1.3051391e-02,
1.0753423e-02, 1.3659596e-03],
[8.1962347e-04, 9.5169604e-01, 1.5044212e-03, ..., 5.1358938e-03,
8.4767938e-03, 3.2877922e-04]], dtype=float32),
'detection_classes': array([4, 4, 4, 4, 1, 4, 1, 1, 1, 4, 1, 1, 4, 6, 6, 4, 2, 6, 7, 7, 4, 6,
1, 6, 7, 4, 7, 9, 4, 2, 6, 7, 1, 3, 9, 6, 1, 6, 6, 3, 2, 5, 1, 2,
3, 6, 1, 1, 8, 1, 1, 1, 7, 4, 3, 1, 9, 6, 6, 8, 9, 6, 8, 2, 8, 7,
2, 6, 1, 6, 9, 5, 2, 9, 9, 3, 1, 9, 8, 5, 9, 9, 3, 5, 6, 1, 6, 6,
9, 6, 6, 1, 2, 1, 2, 9, 9, 4, 4, 7]), 'detection_boxes': array([[5.92004418e-01, 1.69490814e-01, 7.45701075e-01, 2.46565759e-01],
[5.89631081e-01, 2.46157080e-01, 7.39599228e-01, 3.18454713e-01],
[5.87109149e-01, 3.14503819e-01, 7.36972034e-01, 3.85336846e-01],
[5.87837219e-01, 7.05797434e-01, 7.28340387e-01, 7.74214983e-01],
[6.35630414e-02, 1.69735521e-01, 2.04962432e-01, 2.47269154e-01],
[5.92036664e-01, 9.53008384e-02, 7.46890843e-01, 1.73706189e-01],
[6.85142130e-02, 2.45773658e-01, 2.10277155e-01, 3.21099281e-01],
[9.23785418e-02, 7.77337551e-01, 2.25108251e-01, 8.45668435e-01],
[9.24619362e-02, 7.06092656e-01, 2.26126671e-01, 7.77547657e-01],
[5.85118353e-01, 6.37673438e-01, 7.26098835e-01, 7.05848277e-01],
[5.94619289e-02, 9.38070714e-02, 2.03622460e-01, 1.72308475e-01],
[3.44502553e-02, 7.54546002e-03, 2.08990484e-01, 9.39401984e-02],
[5.87913811e-01, 7.74250209e-01, 7.28712976e-01, 8.34733903e-01],
[9.84132528e-01, 3.18221241e-01, 9.96858120e-01, 3.95583898e-01],
[5.87109149e-01, 3.14503819e-01, 7.36972034e-01, 3.85336846e-01],
[5.89539468e-01, 3.49524260e-01, 7.35065162e-01, 4.21008408e-01],
[5.87109149e-01, 3.14503819e-01, 7.36972034e-01, 3.85336846e-01],
[5.89631081e-01, 2.46157080e-01, 7.39599228e-01, 3.18454713e-01],
[1.87163889e-01, 9.88169909e-01, 3.71130943e-01, 9.98176932e-01],
[9.36717317e-02, 7.77330160e-01, 2.24804163e-01, 8.45728278e-01],
[6.63008153e-01, 9.89469707e-01, 8.10642183e-01, 1.00000000e+00],
[9.70665693e-01, 3.16653520e-01, 9.95440483e-01, 3.85887355e-01],
[3.70503038e-01, 2.54840344e-01, 4.76123840e-01, 3.14984292e-01],
[5.87433934e-01, 7.05650687e-01, 7.27492571e-01, 7.73511648e-01],
[9.28397924e-02, 7.06507027e-01, 2.26004675e-01, 7.77664006e-01],
[5.82323313e-01, 1.54982358e-02, 7.39678025e-01, 1.03125945e-01],
[5.87077260e-01, 7.05565095e-01, 7.27602482e-01, 7.74259925e-01],
[9.83516991e-01, 3.11883837e-01, 9.97174442e-01, 3.89778942e-01],
[5.88727355e-01, 6.20116591e-01, 7.30183959e-01, 6.90428734e-01],
[5.89631081e-01, 2.46157080e-01, 7.39599228e-01, 3.18454713e-01],
[5.92004418e-01, 1.69490814e-01, 7.45701075e-01, 2.46565759e-01],
[7.42125034e-01, 0.00000000e+00, 8.81110668e-01, 8.84985179e-03],
[5.44907227e-02, 0.00000000e+00, 2.06223458e-01, 1.28744319e-02],
[9.84132528e-01, 3.18221241e-01, 9.96858120e-01, 3.95583898e-01],
[9.70665693e-01, 3.16653520e-01, 9.95440483e-01, 3.85887355e-01],
[9.84484971e-01, 5.55115521e-01, 9.97788608e-01, 6.32665694e-01],
[7.99783111e-01, 9.75158930e-01, 9.83929038e-01, 9.97444630e-01],
[9.85278428e-01, 5.28306305e-01, 9.97080624e-01, 6.08543932e-01],
[9.88123000e-01, 9.33963358e-02, 9.99226749e-01, 1.72955215e-01],
[9.36717317e-02, 7.77330160e-01, 2.24804163e-01, 8.45728278e-01],
[5.92004418e-01, 1.69490814e-01, 7.45701075e-01, 2.46565759e-01],
[9.08265784e-02, 7.78585851e-01, 2.25109786e-01, 8.43916476e-01],
[7.94785440e-01, 9.86553550e-01, 9.70936120e-01, 9.98435020e-01],
[5.84929466e-01, 7.75964439e-01, 7.24675894e-01, 8.34971726e-01],
[9.70665693e-01, 3.16653520e-01, 9.95440483e-01, 3.85887355e-01], 3.21474910e-01]],
dtype=float32), 'raw_detection_boxes': array([[-0.0132168 , -0.00798112, 0.03437265, 0.02366759],
[-0.01795438, -0.01333077, 0.04313567, 0.03091241],
[-0.00845873, -0.01297706, 0.02555573, 0.02979016],
[-0.01206583, -0.01901898, 0.03632494, 0.04061931],
[-0.01634497, -0.00570066, 0.04027664, 0.01987169],
[-0.02299639, -0.01094626, 0.05078602, 0.02601441],
[-0.01034649, -0.00047059, 0.03106559, 0.04336115],
[-0.01548673, -0.00679935, 0.03944379, 0.05214766],
[-0.00469762, -0.00637354, 0.02257038, 0.05068764],
[-0.00889431, -0.01532986, 0.03383063, 0.06445184],
[-0.01338234, 0.00258018, 0.03299785, 0.03899822],
[-0.02030504, -0.00274394, 0.04193052, 0.04610612],
[-0.0114202 , 0.00825354, 0.0315875 , 0.05609718],
[-0.01720474, 0.00155611, 0.03969076, 0.06473814],
[-0.0055348 , 0.00137738, 0.02347516, 0.06321988],
[-0.0093858 , -0.00954537, 0.03353771, 0.0789085 ],
[-0.01528691, 0.0120711 , 0.03230394, 0.05128276],
[-0.02242971, 0.00611713, 0.04139108, 0.0590462 ],
[-0.01265933, 0.01957938, 0.03226281, 0.06821183],
[-0.0190082 , 0.01264081, 0.04051029, 0.07676097],
[-0.00625486, 0.01262659, 0.02384217, 0.07535952],
[-0.01057751, 0.00036938, 0.03408406, 0.09211845],
[-0.01712188, 0.02387175, 0.03272626, 0.0631646 ],
[-0.02457684, 0.01729448, 0.04191976, 0.07130254],
[-0.01416131, 0.03209703, 0.03322188, 0.08013913],
[-0.02092581, 0.02524993, 0.04159252, 0.08845924],
[-0.00731821, 0.02507119, 0.02447667, 0.08743346],
[-0.01213621, 0.01294496, 0.03459452, 0.10395688],
[-0.01857999, 0.0361888 , 0.03388733, 0.07542843],
[-0.02637036, 0.02969162, 0.04293538, 0.08341235],
[-0.01507254, 0.04520991, 0.03351783, 0.09184141],
[-0.02222046, 0.03861695, 0.04212021, 0.10008947],
[-0.00780608, 0.03797973, 0.02448018, 0.09932629],
[-0.01303079, 0.02687315, 0.03459996, 0.1151351 ],
[-0.0191509 , 0.04890272, 0.03473954, 0.08777986],
[-0.02749499, 0.04277577, 0.04370061, 0.09534387],
[-0.01489433, 0.05867497, 0.03314201, 0.10344677],
[-0.02239214, 0.05207732, 0.04205906, 0.11197228],
[-0.00734611, 0.05139816, 0.02392033, 0.11116292],
[-0.01289164, 0.0412713 , 0.03449183, 0.12679553],
[-0.01872004, 0.06203329, 0.03483813, 0.09988385],
[-0.02761277, 0.05606709, 0.04412681, 0.10715124], 0.2496243 ]],
dtype=float32), 'detection_scores': array([0.9957284 , 0.9954956 , 0.9948391 , 0.9935589 , 0.9928843 ,
0.9922596 , 0.99091065, 0.9904872 , 0.9904753 , 0.9836049 ,
0.97076845, 0.76198786, 0.11483946, 0.08861226, 0.06485316,
0.06403089, 0.06083503, 0.05606595, 0.05304798, 0.05192479,
0.05068725, 0.0497607 , 0.04650801, 0.04170695, 0.04141748,
0.0396772 , 0.03875464, 0.03834933, 0.03700855, 0.03698465,
0.03656569, 0.03464538, 0.03429574, 0.03408125, 0.033981 ,
0.03356522, 0.03337869, 0.03140217, 0.03058183, 0.02957818,
0.02886528, 0.02712101, 0.02674139, 0.02655837, 0.02634463,
0.02611795, 0.02595255, 0.02580112, 0.0251711 , 0.02473494,
0.02423027, 0.02406707, 0.02352765, 0.02347961, 0.02342641,
0.02327773, 0.02312759, 0.0229713 , 0.02272761, 0.02240831,
0.02240023, 0.02203956, 0.02200234, 0.02167007, 0.02112213,
0.0210447 , 0.02079707, 0.02007249, 0.01999336, 0.01993376,
0.01986268, 0.0196887 , 0.01967749, 0.01877454, 0.01874545,
0.01856974, 0.01855248, 0.01853141, 0.01839408, 0.01838818,
0.01830906, 0.01829055, 0.01759666, 0.01758116, 0.01747909,
0.01745978, 0.01728415, 0.01719788, 0.0171611 , 0.01715598,
0.01704106, 0.01684934, 0.01672551, 0.01663077, 0.01645952,
0.01627839, 0.01607156, 0.01592609, 0.01579505, 0.01570672],
dtype=float32), 'num_detections': 100}
Please guyys help me out in this
thanks in advance

Check out his link: https://www.tensorflow.org/api_docs/python/tf/data/Dataset
Here, you can find how to iterate over Dataset using ".as_numpy_iterator()", but also how to use different methods to manipulate the input dataset.
Hope this will be useful.

Related

How to obtain a value from inside a loop

I will be implementing multiprocessing so that the loops are occurring at the same time, but how can I make it so at the end of each iteration, I can obtain the value of westernEurope.cases and easternEurope.cases so that I can add them together
westernEurope = Region("Western Europe", 1000, 0, 0, 8, 4, 4, 0)
while westernEurope.deaths < westernEurope.population:
westernEurope.infection()
if westernEurope.cases > westernEurope.population:
westernEurope.cases = westernEurope.population
print("Infections:", westernEurope.cases)
westernEurope.death()
if westernEurope.deaths > westernEurope.population:
westernEurope.deaths = westernEurope.population
print("Deaths:", westernEurope.deaths)
#where i want to return the value of westernEurope.cases
time.sleep(0.1)
easternEurope = Region("Eastern Europe", 1000, 0, 0, 8, 4, 4, 0)
while easternEurope.deaths < easternEurope.population:
easternEurope.infection()
if easternEurope.cases > easternEurope.population:
easternEurope.cases = easternEurope.population
print("Infections:", easternEurope.cases)
easternEurope.death()
if easternEurope.deaths > easternEurope.population:
easternEurope.deaths = easternEurope.population
print("Deaths:", easternEurope.deaths)
# where i want to return the value of easternEurope.cases
time.sleep(0.1)
print(easternEurope.cases + westernEurope.cases)
IMHO there is no need for multiprocessing. With a generator, your problem can be solved in an even more elgant way.
# where i want to return the value of easternEurope.cases
yield region.cases
Full code:
def desease(region: Region):
while region.deaths < region.population:
region.infection()
if region.cases > region.population:
region.cases = region.population
print("Infections:", region.cases)
region.death()
if region.deaths > region.population:
region.deaths = region.population
print("Deaths:", region.deaths)
# where i want to return the value of easternEurope.cases
yield region.cases
time.sleep(0.1)
easternEurope = Region("Eastern Europe", 1000, 0, 0, 8, 4, 4, 0)
westernEurope = Region("Western Europe", 2000, 0, 0, 8, 4, 4, 0)
eastDesease = desease(easternEurope)
westDesease = desease(westernEurope)
for eastCases, westCases in zip(eastDesease, westDesease):
print(eastCases, westCases)

How to fix "AttributeError: type object has no attribute" in python?

I am adding some code to the preset code to check the time availability, which is if the meeting time can fit into the proposed time schedule. However, I keep getting the following error. Can anyone please give me some advices? Thanks so much for your time.
Preset codes:
from datetime import datetime
class Meeting:
def __init__(self, start_time, end_time):
self.start_time = start_time
self.end_time = end_time
My codes:
def check_availability(meetings, proposed_time):
meeting_start = Meeting.datetime.start_time.hour
meeting_end = Meeting.datetime.end_time.hour
ok_time = datetime.proposed_time.hour
if meeting_start < ok_time < meeting_end:
return True
else:
return False
meetings = [Meeting(datetime(2018, 8, 1, 9, 0, 0), datetime(2018, 8, 1, 11,
0, 0)), Meeting(datetime(2018, 8, 1, 15, 0, 0), datetime(2018, 8, 1, 16, 0,
0)), Meeting(datetime(2018, 8, 2, 9, 0, 0), datetime(2018, 8, 2, 10, 0, 0))]
print(check_availability(meetings, datetime(2018, 8, 1, 12, 0, 0)))
print(check_availability(meetings, datetime(2018, 8, 1, 10, 0, 0)))
Your code raises this exception:
AttributeError: type object 'Meeting' has no attribute 'datetime'
At this line:
meeting_start = Meeting.datetime.start_time.hour
Python is telling you that the Meeting class doesn't have an attribute named datetime. This is true: the Meeting class is a factory for making meeting objects (or instances), and these objects have start_time and end_time attributes, which are set by passing datetime instances to Meeting's __init__ method. These attributes can be accessed like this:
>>> meeting = Meeting(datetime(2018, 8, 1, 9, 0, 0), datetime(2018, 8, 1, 11,
0, 0))
>>> print(meeting.start_time)
2018-08-01 09:00:00
>>> print(meeting.end_time)
2018-08-01 11:00:00
Your check_availability function is being passed a list of meetings, so you need to loop over the list to check whether any of the meetings conflict with the proposed meeting time.
def check_availability(meetings, proposed_time):
# Loop over the list of meetings; "meeting"
# is the meeting that you are currently inspecting.
for meeting in meetings:
# if proposed_time is between meeting.start_time
# and meeting.end_time, return False
# If you get through the list without returning False
# then the proposed time must be ok, so return True.

Changing keys in dictionary

How can I change the keys of a dictionary of lists to display dates (outright) rather than datetime.date(2017, 1, 1) which is what they currently are formatted as?
Example of section of list:
{datetime.date(2017, 9, 7): [162.3, 163.24, 162.22, 163.18], datetime.date(2017, 7, 10): [160.44, 161.13, 160.44, 160.94],
I am rather new to python so any help would be much appreciated. Thanks.
call the strftime method for each datetime object:
import datetime
s = {datetime.date(2017, 9, 7): [162.3, 163.24, 162.22, 163.18], datetime.date(2017, 7, 10): [160.44, 161.13, 160.44, 160.94]}
new_s = {a.strftime("%Y-%m-%d"):b for a, b in s.items()}
Output:
{'2017-07-10': [160.44, 161.13, 160.44, 160.94], '2017-09-07': [162.3, 163.24, 162.22, 163.18]}
Try this:
import datetime
my_dict = {datetime.date(2017, 9, 7): [162.3, 163.24, 162.22, 163.18], datetime.date(2017, 7, 10): [160.44, 161.13, 160.44, 160.94]}
new_dict = {str(k): v for k,v in my_dict.items()}
Output:
{'2017-07-10': [160.44, 161.13, 160.44, 160.94],
'2017-09-07': [162.3, 163.24, 162.22, 163.18]}

How to use a loop to create radiobuttons in Python

I have to create multiple radio buttons. Each has its own name and location on grid. There are also several variables involved.
I stored all the data to create these radio buttons in a tuple
(I know that FONTS does not cause a problem, its stored above this but isn't shown here):
self.unitType = IntVar()
self.matPropSel = IntVar()
self.secProfSel = IntVar()
self.endSupSel = IntVar()
self.loadTypeSel = IntVar()
self.RADIOLABELS = ( # Text, Font, Variable, Value, Row, Column
('English', self.FONTS[3], self.unitType, 1, 3, 1),
('metric', self.FONTS[3], self.unitType, 2, 4, 1),
('select preset', self.FONTS[3], self.matPropSel, 1, 6, 1),
('manual entry', self.FONTS[3], self.matPropSel, 2, 7, 1),
('select preset', self.FONTS[3], self.secProfSel, 1, 10, 1),
('manual entry', self.FONTS[3], self.secProfSel, 2, 11, 1),
('one end', self.FONTS[3], self.endSupSel, 1, 15, 1),
('both ends', self.FONTS[3], self.endSupSel, 2, 16, 1),
('point', self.FONTS[3], self.loadTypeSel, 1, 18, 1),
('uniform distribution', self.FONTS[3], self.loadTypeSel, 2, 19, 1),
('uniform variation', self.FONTS[3], self.loadTypeSel, 3, 20, 1)
)
So, how would I use a for loop to go through this tuple and generate a radio button from each line? Do all of the variables have to be the same? I am having problems with them.
Here is my attempt at a loop:
outerIndexer = 0
for labels in self.RADIOLABELS:
Radiobutton(self, text=self.RADIOLABELS[outerIndexer][0], font=self.RADIOLABELS[outerIndexer][1],
variable=self.RADIOLABELS[outerIndexer][2], value=self.RADIOLABELS[outerIndexer][3])\
.grid(row=self.RADIOLABELS[outerIndexer][4], column=self.RADIOLABELS[outerIndexer][5])
outerIndexer += 1
You can do it like this by looping through the RADIOLABELS. Note: it is also recommended to save the button to a list so it doesn't get lost.
self.RADIOLABELS = ( # Text, Font, Variable, Value, Row, Column
('English', self.FONTS[3], self.unitType, 1, 3, 1),
('metric', self.FONTS[3], self.unitType, 2, 4, 1),
('select preset', self.FONTS[3], self.matPropSel, 1, 6, 1),
('manual entry', self.FONTS[3], self.matPropSel, 2, 7, 1),
('select preset', self.FONTS[3], self.secProfSel, 1, 10, 1),
('manual entry', self.FONTS[3], self.secProfSel, 2, 11, 1),
('one end', self.FONTS[3], self.endSupSel, 1, 15, 1),
('both ends', self.FONTS[3], self.endSupSel, 2, 16, 1),
('point', self.FONTS[3], self.loadTypeSel, 1, 18, 1),
('uniform distribution', self.FONTS[3], self.loadTypeSel, 2, 19, 1),
('uniform variation', self.FONTS[3], self.loadTypeSel, 3, 20, 1)
)
radiobuttons = []
for _Text, _Font, _Variable, _Value, _Row, _Column in self.RADIOLABELS: # it will unpack the values during each iteration
_Radio = tk.Radiobutton(root, text = _Text, font = _Font, variable = _Variable, value = _Value)
# ^ root should be the frame/Tk you're trying to place it on, it will be self if this is a direct subclass of a Frame or Tk
# ^ or Radiobutton(.....) if you did from tkinter import *
_Radio.grid(row = _Row, column = _Column)
radiobuttons.append(_Radio)

Why does this Python code get the name error? [duplicate]

This question already has answers here:
Accessing class variables from a list comprehension in the class definition
(8 answers)
Closed 7 years ago.
I want to have attribute that is shared by and accessible to all instances of a class.
Sample code:
class Test:
code = [ 1, 2, 3, 5, 6, 7, 8, 9, 10]
code2d = [ [ code[j*3 + i] for i in range(3) ] for j in range(3) ]
def __init__(self):
pass
However, I get error:
NameError: name 'code' is not defined
If I put the lines with code and code2d into init method, all is working properly.
Why this code throws the error?
code is a class variable , and hence when accessing it you need to call - Test.code , you cannot access them using code .
Also, even if you do access them using Test.code , it would still not work, because the value for class variables (default value) is calculated when the class is being defined, so when you try to access Test.code , you would not be able to access Test as it has not been created yet. Example of that -
>>> class Test:
... code = [ 1, 2, 3, 5, 6, 7, 8, 9, 10]
... code2d = [ [ Test.code[j*3 + i] for i in range(3) ] for j in range(3) ]
... def __init__(self):
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in Test
File "<stdin>", line 3, in <listcomp>
File "<stdin>", line 3, in <listcomp>
NameError: name 'Test' is not defined
I am guessing when you put them in the __init__() you are putting them as -
class Test:
def __init__(self):
code = [ 1, 2, 3, 5, 6, 7, 8, 9, 10]
code2d = [ [ code[j*3 + i] for i in range(3) ] for j in range(3) ]
This would work, because code here is a local variable and hence can be accesed by the other local variables in the __init__() method , though they would not be accessible outside this function.
Maybe, you do not need them as class variables , if you want to have code and code2d for all instances of the class (objects of the class) , you should create instance variable as-
>>> class Test:
... def __init__(self):
... self.code = [ 1, 2, 3, 5, 6, 7, 8, 9, 10]
... self.code2d = [ [ self.code[j*3 + i] for i in range(3) ] for j in range(3) ]
If you really want code and code2d to be class variables , one way to do it would be to define code2d outside the class Test , example -
class Test:
code = [ 1, 2, 3, 5, 6, 7, 8, 9, 10]
def __init__(self):
pass
Test.code2d = [ [ Test.code[j*3 + i] for i in range(3) ] for j in range(3) ]

Categories

Resources