UGX-Mods Login

or login with an authentication provider below
Sign In with Google
Sign In with Twitter
Sign In with Discord
Sign In with Steam
Sign In with Facebook
Sign In with Twitch

Python Classes. ( Or classes in general)

broken avatar :(
Created 10 years ago
by Deleted User
0 Members and 1 Guest are viewing this topic.
1,327 views
broken avatar :(
  • Exofile
  • Deleted Member
×
broken avatar :(
Exofile
This user is deleted :(
I'm working on another small game in Python. I know the language has big limitations etc. But It teaches me simple programming, and I enjoy it. So I'm trying to make a bunch of "Items", such as weapons etc for the game. Now I could make a list, and have a value for what  type of item, "damage", "value" etc. But I've heard that using classes is a better method for doing it. I've looked up classes several times, but I can't seem to wrap my head around it?

I see things like self__init__ or however it looks. I don't understand why you need this, and in general how classes work. Most likely, I'm just not good enough to understand how it works, so does anyone here know a simple way to explain how it works + how to use it?

Any response is appreciated, as always. Thanks. ;)
Last Edit: January 25, 2016, 11:58:20 am by Exofile
broken avatar :(
  • DeletedUser
  • Deleted Member
×
broken avatar :(
DeletedUser
This user is deleted :(
Source: https://docs.python.org/2/tutorial/classes.html
Classes are like data structures which make it easier to store informations.
An example class in python taken from the page:
Code Snippet
Plaintext
class MyClass:
    """A simple example class"""
    i = 12345
    def f(self):
        return 'hello world'
So, for example, if you want to create a class for storing weapon info, you could do this:
Code Snippet
Plaintext
class Weapon:
    damage = 20
    ammo = 80
    clip = 8
    name = "Example Weapon"
    icon = "pic/example.png"
How you assign it to a var.
Code Snippet
Plaintext
x = Weapon()
Bear in mind, that everytime you assign the class to a var, the values we specified are going to be assigned to the attribute references.
If you were to "define" the weapon attributes directly via that function, the class needs a __init__ function like the one below:
Code Snippet
Plaintext
def __init__(self,name,damage,clip,ammo,icon):
    self.name = name
    self.damage = damage
    self.clip = clip
    self.ammo = ammo
    self.icon = icon
Example usage:
Code Snippet
Plaintext
x = Weapon("Colt 1911", 20, 8, 80, "pic/Colt1911.png")
That way, we've assigned a class to our variable and assigned our values to it.
Example of accessing the values:
Code Snippet
Plaintext
print x.name
the following will print
Code Snippet
Plaintext
Colt 1911
Similar thing is for other attributes.
Hope this helped.
broken avatar :(
  • Exofile
  • Deleted Member
×
broken avatar :(
Exofile
This user is deleted :(
Source: https://docs.python.org/2/tutorial/classes.html
Classes are like data structures which make it easier to store informations.
An example class in python taken from the page:

I had looked up that tutorial many times before, never got my head around understanding it properly. Appreciate the response, I think I understand what they're used for now. Essentially, doing a __init__ function makes each var in the class turn into a dictionary, where I can access the information stored, like a chest holding tons of vars.

Source for Dictionary:

https://docs.python.org/2/tutorial/datastructures.html#dictionaries

 
Loading ...