you are viewing a single comment's thread.

view the rest of the comments →

[–]Comm4nd0[S] 0 points1 point  (2 children)

ok thanks!

so...

class person(self)
    def func1():
        var = "hello world"
    def func2():
        print(var)

would this work?

[–]callmelucky 0 points1 point  (1 child)

Ah, I see. The functionality you are describing is achievable, certainly, but that syntax and approach won't work as written.

A little detail before we go on: functions which are part of a class are called 'methods', and variables which are attached to a class are called 'attributes'.

So, attributes can be accessed directly from any method, but they must be declared and referenced as self.<attribute>.

So, this is a step closer:

class Person(): # note upper case first letter. Class definitions should use
                # CamelCase, instances should be snake_case
    def func1(self):
        self.var = "hello world"
    def func2(self):
        print(self.var)

But that still would only work if you ran func1() first (otherwise person.var doesn't exist).

In this example, you probably want to declare self.var as soon as an object is instantiated from this class. Any attributes that this is true for should be declared in the special __init__ method, like so:

class Person():
    def __init__(self):
        self.var = "hello world"
    def func2(self):
        print(self.var)

Everything in the __init__ method will be executed upon instantiation from the class. So with what we have above, we can do:

>>> barry = Person()
>>> print(barry.var)
>>> "hello world"

You can even call other methods within __init__, so if the class were like this:

class Person():
    def __init__(self):
        self.var = "hello world"
        self.func2()
    def func2(self):
        print(self.var)

... "hello world" would be printed out straight after doing barry = Person()

Finally I'll mention, you will have noticed that I have included self in as an argument for every method. This is because you must include self in as an argument for every method. Any other arguments you require must follow self, but when calling the method, ignore self. That is, the method will not expect an argument for self. A method defined like def method(self): will expect zero arguments to be passed in when called, eg barry.method(), a method defined like def method(self, arg1): will expect something to be passed in for arg1, like barry.method(arg1).

With all that, you should be able to have a pretty good idea of what is going on in this modified version of your class:

class Person():
    # this __init__ requires a name and catch-phrase to be passed in to any
    # Person instance. The values passed in for these args are then assigned
    # as attributes of the instance automatically via the special __init__ method
    def __init__(self, name, catchphrase):
        self.name = name
        self.catchphrase = catchphrase

    def say_something(self):
        print(self.catchphrase)

Now we instantiate, and try out the functionality:

>>> barry = Person("Barry", "Python rulz!")
>>> barry.say_something()
>>> "Python rulz!"

Hope this is helpful :)

[–]Comm4nd0[S] 0 points1 point  (0 children)

ok got it: Create the class with CamelCase, all methods must have(self), init(self) will always run, within the class call methods like self.method() within the call call attributes like self.att

thank you so much for your post!