1

I'm quite new to Python oop. So, this class:

class Foo:
    def barfunc(self, baz):
        print baz

looks to me quite similar to

class Foo:
    def barfunc(self, baz):
        self.baz = baz            
        print self.baz

Output will, I think be the same, so I would like to know which is preferred and why. Maybe I miss major possible pitfalls from using one or another.

3
  • You have multiple syntax errors... (class declaration doesn't end with (), unless you're inheriting from something.. i.e. class Foo(object):. function declaration should end with :...). Further, class name should begin with capital case. Commented Feb 8, 2015 at 17:27
  • Have you been through the Classes section of The Tutorial? Commented Feb 8, 2015 at 17:27
  • @ alfasin: ty, yes, I tiped it too quickly. Hopefully the question still shows through. Commented Feb 8, 2015 at 17:29

3 Answers 3

2

If your aim is to just print the baz once, then both are equivalent.

However, if you want to re-use baz later on in your code, then using the second one is the better approach, because you can add additional methods to access it now.

class Foo(object):
    def barfunc(self, baz):
        self.baz = baz            
        print self.baz

    def barbar(self):
        print self.baz

And now you can do

>>> f = Foo()
>>> f.barfunc(10)
10
>>> f.barbar()
10
Sign up to request clarification or add additional context in comments.

1 Comment

Oops, excuse me, but, for the record, there's a slight mistake in your example code, it should be: >>>f=Foo() >>> f.barfunc(10) >>> f.barbar()
2

One major pitfall is your terminology - neither example shows a class variable.

In the first example, baz is an argument to an instance method and baz is only available in the local scope of that method.

In the second example, baz is an argument to an instance method and is assigned to an instance attribute, self.baz, within that method. After the method has been called, self.baz will be available to all objects in the class instance.

2 Comments

ty, yes, i know i really suck at this. I'm quite overwhelmed with the terminology, but one great improvement over just yesterday is that I actually understood your post... :)
I would say that there are two separate ideas to learn : 1) what OOP is as a programming style, what its advantages are, how to structure a program using that style; and 2) how you implement that programming style in Python, the tools Python offers and how to use those tools. Pyvideo.org is an excellent resource - Python's Class Development Toolkit comes to mind.
0

the first example will have effect only inside the function..but the second one will have effect in the entire class..

class foo():
    bar=1
    def baz(self,bar):
        print bar
    def baz1(self,bar):
        self.bar=bar
a=foo()
a.bar
#prints 1
a.baz(2)
#prints 2
a.bar
#here it prints 1 again because it is the class variable which is not affected by the baz() function's local variable
a.baz1(3)
#prints 3
a.bar
#here the class variable value changes to 3 because self.bar changes the value of bar in entire class

self is the entire instance of the class and it changes every value of the class when used that is it has a global scope or scope in the entire class.

run this example and ask if you have any doubt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.