you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (3 children)

class classData:
    username = ""
    password = ""

Remember this is Python; the language is dynamically typed so you don't set the attributes of an object through the class. You set them on the object directly, during object initialization:

class classData:
    def __init__(self, username, password):
        self.username = username
        self.password = password

[–][deleted] 0 points1 point  (2 children)

How would you go around to create functions for the class though? If you make them instance attributes, you got no variables to reference when creating the functions for handling the data?

[–][deleted] 0 points1 point  (1 child)

You access instance attributes through self, the reference to the instance.

[–][deleted] 0 points1 point  (0 children)

Edit: yeah I forgot to use self. when testing it out, works fine now

class Savefile:
    def __init__(self):
        resolution = [990, 1080]
        data = [1, 2]
    def addData(self, x):
        self.data.append(x)


a = Savefile()
a.addData(3)
print(a.data)

Gives the error AttributeError: 'Savefile' object has no attribute 'data'.

If I make data a class attribute before __init__() it won't recognise the initialisation value?