0

In C++, I can do something like

struct sample{
char name[100];
int roll;
}x[100];

x[0].roll=5;
x[0].name="Asdfgh";

Here I can add a maximum of 100 elements, and change the value of their attributes whenever I want.

What python code that achieves the same thing?

I'm assuming namedtuples will not work in this way because it is immutable.

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Aswin G
  • 97
  • 2
  • 8
  • 1
    Python people usually just throw dicts around. `x = [{'roll': 5, 'name': 'Asdfhg'}]` – Chris Martin Apr 15 '16 at 09:47
  • Why don't you use a custom class or dictionary? – falsetru Apr 15 '16 at 09:47
  • @ChrisMartin that's a horrible practice, since dicts aren't structured data, just containers. But sadly it is prevalent. – Ilja Everilä Apr 15 '16 at 09:48
  • @Ilja How is using a dict for this purpose different from a struct? – Selcuk Apr 15 '16 at 09:49
  • @Selcuk when I see the definition of a struct I know exactly what it is. When I see a bunch of dictionaries, I have to use them and hope for the best. `dict.get` eases the pain, but still using namedtuples and classes when you do know your data is not such a bad idea. – Ilja Everilä Apr 15 '16 at 09:51
  • `a = [{'roll': 0, 'name': ''}] * 100` :) – Aesthete Apr 15 '16 at 09:52
  • @Ilja Python is not strongly typed as C++ is. How do you know the data types of the properties in a Python class without seeing its usage or comments? – Selcuk Apr 15 '16 at 09:53
  • The same way I see the structure of a C struct: I read the docs or the source. – Ilja Everilä Apr 15 '16 at 09:53
  • @Aesthete that has a funny gotcha: it creates a list of 100 references to the same dictionary. Mutate 1, observe the wackiness... – Ilja Everilä Apr 15 '16 at 09:54
  • General observation: don't write C++ in Python. – cdarke Apr 15 '16 at 09:55
  • @Ilja You are missing the point. With a C struct you _have to_ declare the types, that's why you can tell just by looking at the struct definition. You can't decide which properties are of which type by looking at the source of a Python class. – Selcuk Apr 15 '16 at 09:56
  • @Selcuk that depends on the level of documentation etc. And yes, we can break everything in python, if we want to. Don't do it though. The thing I mean is that a `dict` is a mapping of keys and values. I have no idea what keys and values it might contain in general and usually that's the point. Promises can be made, but then you're a-ok making those same promises with a namedtuple or a class. – Ilja Everilä Apr 15 '16 at 09:58
  • And to make one thing clear: I do use dicts as maps of keys to values that I know are there beforehand. Lot of stuff returns dicts with predefined keys. But I'm trying to argue that sometimes a namedtuple etc. is cleaner. – Ilja Everilä Apr 15 '16 at 10:01
  • Actually, classes in python are quite dict-ish; their 'keys' are just restricted to valid identifiers. It's better though if you prefer `obj.a` over `obj["a"]`. – Miles Apr 15 '16 at 10:03
  • Yes, I'm aware of `__dict__`. – Ilja Everilä Apr 15 '16 at 10:04
  • Also see #9 in [Python Progression Path](http://stackoverflow.com/a/2576240/2011147) (Yes, I know this is meant to be a joke). – Selcuk Apr 15 '16 at 10:05

3 Answers3

0

In Python you can use dictionnaries. Example :

sample = {}
sample['role'] = 5
sample['name'] = "Asdfgh"

and to display the name of the sample for example do the following :

print(sample['name'])

Here you do not define a maximum of elements.

Julien Salinas
  • 1,059
  • 1
  • 10
  • 23
0

2 simple examples

first (dicts):

x = list()
x.append({'name':'bob', 'roll': 5}) # or x.append(dict())
x[0]['name'] = 'giul'
x[0]['roll'] = 4

second (classes):

class X:
    name = 'bob'
    roll = 4

c = list()
c.append(X())
c[0].name = 'sam'
c[0].roll = 10
nonsensei
  • 480
  • 4
  • 15
0

This can do your job.

x=[{'roll': 0, 'name': ''}] * 100
x[0]["roll"]=5;
x[0]["name"]="Asdfgh";

print(x[0]["roll"])
print(x[0]["name"])
arshovon
  • 13,270
  • 9
  • 51
  • 69