-1

I am starting to study class in python, and am trying to get my head around the concepts of attributes, methods and parameters in OOP.

I am working with 3 examples:


example 1

   class Clock(object):
        def __init__(self, time):
        self.time = time
        def print_time(self):
        time = '6:30'
        print self.time

with:

clock = Clock('5:30')
clock.print_time()

It prints 5:30


example 2

class Clock(object):
    def __init__(self, time):
    self.time = time
    def print_time(self, time):
    print time

with:

clock = Clock('5:30')
clock.print_time('10:30')

It prints 10:30.


example 3

  class Clock(object):
        def __init__(self, time):
        self.time = time
        def print_time(self):
        print self.time

finally, with:

boston_clock = Clock('5:30')
paris_clock = boston_clock
paris_clock.time = '10:30'
boston_clock.print_time()

It prints 10:30

could please someone explain me how attributes, methods and parameters are being bound to objects in these examples?

8-Bit Borges
  • 9,643
  • 29
  • 101
  • 198
  • 1
    I'm voting to close this question as off-topic because SO is not a tutoring service. – TigerhawkT3 Dec 30 '16 at 02:44
  • 3
    Your question is rather vague. Attributes, methods, and parameters are very general concepts. I think you need to narrow down the area that you need to understand. Right now it's not even clear that you know what attributes, methods, and parameters are. It might be good to make your question of the form "I think that blah is a parameter and is being bound to an object in such-and-such line of code, is that correct?" That way people can see what your existing understanding of the concepts is. – BrenBarn Dec 30 '16 at 02:46
  • Yes. As @BrenBarn has already said, you question is very broad as of now. Is there something specific about Python's OOP concepts that is confusing you? Instance variables? attribution look-up? method calls? object creation? – Christian Dean Dec 30 '16 at 02:48
  • Try reading this first (the UC Berkeley intro to cs class textbook) about classes: http://composingprograms.com/pages/25-object-oriented-programming.html Then maybe you might have more specific questions to ask. – kbunarjo Dec 30 '16 at 02:49
  • @kbunarjo nice link, will look into it, thanks – 8-Bit Borges Dec 30 '16 at 02:52
  • I suspect that you haven't copied the formatting of your code correctly, as most of the example code you've posted won't run. – Tagc Dec 30 '16 at 02:52
  • Possible duplicate of [python bound and unbound method object](http://stackoverflow.com/questions/13348031/python-bound-and-unbound-method-object) – Christian Dean Dec 30 '16 at 02:55
  • This is not a duplicate of that question, @leaf. That question is about bound-unbound methods. This is about the general of "methods", "attributes", and "parameters" in OOP languages. – Preston Hager Dec 30 '16 at 02:57
  • @PrestonHager That is currently, the best duplicate target I could find. Feel free to post your own. – Christian Dean Dec 30 '16 at 02:59
  • 2
    You don't need to mark a question as a duplicate if it hasn't been asked before. However, it may need to be closed for a different reason, i.e. "too broad". – Preston Hager Dec 30 '16 at 03:01
  • @data_garden You might find this useful: https://jeffknupp.com/blog/2014/06/18/improve-your-python-python-classes-and-object-oriented-programming/ IMHO, anyone approaching learning classes and OOP thinking should read this. – Chris Larson Dec 30 '16 at 03:14

2 Answers2

2

Attributes are the variables within a class or instance. In something like this the variable hello is an attribute of the class Hi.

class Hi:
  hello = "Hello World!"

Methods are functions within the class, so for something like this, function greet is a method of the class Hi.

class Hi:
  def greet(self):
    pass

Parameters are input(s) that go into a method. So the string, "Hello World!" is a parameter of the method say in the class Hi.

class Hi:
  def say(self, saying):
    print(saying)

Hi().say("Hello World!")

There's a nice question on the Software Engineering StackExchange site about OOPL. Explaining OOP Concepts to a non technical person.

Community
  • 1
  • 1
Preston Hager
  • 1,514
  • 18
  • 33
  • 1
    _"could please someone explain me how attributes, methods and parameters are being bound to objects in these examples?"_ - I really don't think your answering that question. Seems more like your beating around the bush to me. No offense. – Christian Dean Dec 30 '16 at 03:00
  • @leaf Makes sense, I was just trying to explain how each one works in relation to standard programming. The question at the end of the post may work better for explaining the concepts. – Preston Hager Dec 30 '16 at 03:03
  • Yeah, I can see where you are coming from. However, the OP seems thoroughly confused, and could really benefit from a good explanation. alas, I cannot find a suitable answer which answer to his question. mabye I'll post my own. – Christian Dean Dec 30 '16 at 03:06
  • @leaf - The OP's question is far too broad and needs something along the lines of a tutorial on OOP. – TigerhawkT3 Dec 30 '16 at 03:12
2

This link here can explain more about class attributes/methods/parameters.

However, I do understand how complicated these concepts are, so I will answer your question (although in the future, try to ask a more specific question!).

In example one:

init (the initializer) and print_time are both class attributes. When you initialize the clock variable and pass in the parameter '5:30', it accesses the init function and hits the self.time = time line of code. Since time is accessed using a dot notation, time is an INSTANCE attribute (specific to the individual object).

When you call self.print_time(), the time there is a local variable specific to the function call, therefore the instance attribute is not changed. That is why when you print self.time it is still 5:30.

In example two:

In this case, the init and print_time functions are both class attributes (similar to the example above). The initialization of the clock object is the same as above. However, when it calls print_time, time is a the parameter '10:30', and therefore when we just print time (notice we did not use any dot notation), it prints only the local variable of '10:30'.

In example three:

init and print_time are both class attributes, same as the above two examples. When you initialize the boston_clock object, it is similar to both example one and two. Then you assign the name paris_clock to the object boston_block (notice that paris_clock and boston_clock are just names pointing to the same object, like how I could have two names). Therefore when we execute the line of code paris_clock.time = '10:30', the INSTANCE attribute of this single object is changed to '10:30'. However, since boston_clock is pointing to the same object as paris_clock, boston_clock's time attribute is also '10:30'.

kbunarjo
  • 1,277
  • 2
  • 11
  • 27