I've been playing a bit around with Python, and classes in python.
[code]
class Entity:
"""The base entity upon which all entites are based."""
name = "ENT_BASE"
type = "BASE"
def __init__(self):
self.data = []
class Weapon:
"""Base weapon entity."""
base = "ENT_BASE"
name = "WEAPON_BASE"
type = "BASE"
def __init__(self):
self.data = []
[/code]
Now the code above does nothing in itself. What i'm asking is, how do i make an external python script read all the 'name' in each class? Or just "count" the classes?
If you save this as "entity.py" for example you can import the file as a module by putting that external python script in the same folder and then just use
[code]
import entity
[/code]
and you're done
[QUOTE=Denzo;19199506]If you save this as "entity.py" for example you can import the file as a module by putting that external python script in the same folder and then just use
[code]
import entity
[/code]
and you're done[/QUOTE]
Yeah, i'm aware of that. But how would i create, say a 'table' of all the classes?
How much oop programming have you done? Because the code you have shown doesn't look very oop! You say that Entity is the base, and yet do not use subclassing.
Also, a class is a type. You can use the isinstance function in python to determine if an object is of a particular type, rather than having to qualify it with strings.
Having name as a property of the class is also strange or ambiguous. A class is it's name essentially. The name of class Dog is Dog - these aren't filled in automatically or anything, but what you have shown simply does not really resemble properly done oop with python.
I'm not just getting at you, I just want to make sure you understand the principles behind oop, and how it works with python.
[editline]08:40PM[/editline]
And the question you are asking - there seems to be no use to it. You can use the inspect module to do this, but the only reason I can think of for doing so is documentation.
If you wish to provide the user with a list of different classes you can just say
[code]
classes = [Entity, Weapon]
[/code]
But I cannot see why you would want to do this either!
[QUOTE=TheBoff;19199800]
And the question you are asking - there seems to be no use to it. You can use the inspect module to do this, but the only reason I can think of for doing so is documentation.
If you wish to provide the user with a list of different classes you can just say
[code]
classes = [Entity, Weapon]
[/code]
But I cannot see why you would want to do this either![/QUOTE]
Firstly, there's no real use of this. I'm just trying things out in Python, randomly.
Second, the above code IS what i want, in a way. Only that it should count the classes in a dynamic way, instead of static like above. So if i add any classes, they'd be added automatically.
[editline]09:44PM[/editline]
The important part is just the
[code]
class Entity:
#stuff
class Weapon:
#stuff
[/code]
part. Everything else is just for content.
Yeah, you can use the inspect module. All I am saying is that there is no real life usage for this, other than documentation. I'll be a second, whilst I test out the exact syntax in a console, but that's how you'd do it.
I would recommend you do actually try to write something! The first things you write will inevitably shit, but it's the learning that counts! You will learn more trying to acheive a real, and somewhat useful goal than just playing.
My first real python project was a noughts and crosses game, and I wrote several more tiny, tiny apps before that.
[b]Here we Go![/b]
We're assuming you've saved that code in entity.py
To do this, you would do:
[code]
import inspect
import entity
classes = [member[1] for member in inspect.getmembers(entity, inspect.isclass)]
[/code]
Which would produces the same classes as before. That's my fairly advanced python way of doing it - if you're just starting, this has the same effect but should look less daunting!
[code]
import inspect
import entity
classes = []
for member in inspect.getmembers(entity):
if inspect.isclass(entity[1]):
classes.append(entity[1])
[/code]
It is best if you try this out in the python console, and try to work out what each step of it does for yourself. Use the documentation [url]http://docs.python.org/3.1/library/inspect.html[/url] !
If you really can't, I can explain parts of the code for you.
That is exactly what i want to do! Thanks! But this outputs an error:
[code]
Traceback (most recent call last):
File "[code above].py", line 6, in (module)
if inspect.isclass(entity[1]):
TypeError: 'module' object is not subscriptable
[/code]
[editline]02:05PM[/editline]
The first one works fine, but the second is 'faulty'.
Hahahah. I tested the first one, but the second was off the top of my head.
Yeah, every mention of entity in the second for loop should be a 'member'.
[code]
import inspect
import entity
classes = []
for member in inspect.getmembers(entity):
if inspect.isclass(member[1]):
classes.append(member[1])
[/code]
[QUOTE=TheBoff;19210868]Hahahah. I tested the first one, but the second was off the top of my head.
Yeah, every mention of entity in the second for loop should be a 'member'.
[code]
import inspect
import entity
classes = []
for member in inspect.getmembers(entity):
if inspect.isclass(member[1]):
classes.append(member[1])
[/code][/QUOTE]
Now it makes sense, thanks! :)
Sorry, you need to Log In to post a reply to this thread.