ynonp / python-examples-verint-2016-07

Python examples and exercises
MIT License
2 stars 35 forks source link

Second question #182

Open zfigov opened 8 years ago

zfigov commented 8 years ago

How can I know that I am creating another instance of the class? ( static variable in C++)

ynonp commented 8 years ago

C++ static variables == Python class variables: https://www.tocode.co.il/bundles/python-verint/lessons/class-syntax?tab=article

(called משתני מחלקה in the article)

zfigov commented 8 years ago

I read the text but still didn't get it. Is a new instance created every time you call the object- MyCounter()? Seems that it isn't if so what does it mean when you just call the object without parameters?

Thanks Zvi

On Wed, Jul 20, 2016 at 8:59 PM, ynonp notifications@github.com wrote:

C++ static variables == Python class variables:

https://www.tocode.co.il/bundles/python-verint/lessons/class-syntax?tab=article

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ynonp/python-examples-verint-2016-07/issues/182#issuecomment-234029654, or mute the thread https://github.com/notifications/unsubscribe-auth/ATWpyHFOT4ifzCGzG3zugx4xn6laCkDqks5qXmH5gaJpZM4JQ2vz .

ynonp commented 8 years ago

Every time you call a constructor function (i.e. MyCounter in your example) a new object is created. For example the following code prints "Hello" 3 times:

class HelloWorld(object):
    def __init__(self):
        print "Hello World"

a = HelloWorld()
b = HelloWorld()
c = HelloWorld()

Python calls __init__ magic method automatically each time a new instance is created and its job is to initialize data relevant to that instance (or other required system data).

In the counter example you can use __init__ to increase a counter stored as a class variable, thus keeping track of newly created objects

zfigov commented 8 years ago

But how do I know to initialize the first time? Is there some kind of indication that this is the first time the object is initialized?

On Thu, Jul 21, 2016 at 8:21 AM, ynonp notifications@github.com wrote:

Every time you call a constructor function (i.e. MyCounter in your example) a new object is created. For example the following code prints "Hello" 3 times:

class HelloWorld(object): def init(self): print "Hello World"

a = HelloWorld() b = HelloWorld() c = HelloWorld()

Python calls init magic method automatically each time a new instance is created and its job is to initialize data relevant to that instance (or other required system data).

In the counter example you can use init to increase a counter stored as a class variable, thus keeping track of newly created objects

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ynonp/python-examples-verint-2016-07/issues/182#issuecomment-234159541, or mute the thread https://github.com/notifications/unsubscribe-auth/ATWpyDXpCaqD9FeLruv8O8YRjxdUuFaBks5qXwHYgaJpZM4JQ2vz .

ynonp commented 8 years ago

That's what class variables are for. Here's a slightly modified version. This works because the variable s_text is initialised once before creating any objects

class HelloWorld(object):
    s_text = "Hello World"

    def __init__(self):
        print HelloWorld.s_text

a = HelloWorld()
b = HelloWorld()
c = HelloWorld()
zfigov commented 8 years ago

Ok, now I got it (I sent you solution).

class MyCounter(object): count = 0 # this is only called once for all objects created def init(self): MyCounter.count += 1 # this uses the class property that is used in all the objects created In c1 = MyCounter() the c1 isn't used (we don't need to return a value)

Thanks

On Thu, Jul 21, 2016 at 8:34 AM, ynonp notifications@github.com wrote:

That's what class variables are for. Here's a slightly modified version. This works because the variable s_text is initialised once before creating any objects

class HelloWorld(object): s_text = "Hello World"

def __init__(self):
    print HelloWorld.s_text

a = HelloWorld() b = HelloWorld() c = HelloWorld()

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ynonp/python-examples-verint-2016-07/issues/182#issuecomment-234161086, or mute the thread https://github.com/notifications/unsubscribe-auth/ATWpyHkR0ue6LwLLotAhZ1-7pfYMhFPoks5qXwTZgaJpZM4JQ2vz .