python: differences between init method and in-class declare?

dzhwinter

is there any difference between these two method? option1 :

class a(object):
    def __init__(self):
        self.x = 123
        self.y = 345

option2 :

class a(object):
        x = 123
        y = 345

is there any difference between these two options? Thanks in advance.

Martin Konecny

An example of the first method (instance level variables):

instance0 = a()
instance1 = b()

instance0.x = 5
print instance1.x # prints 123

print a.x  # undefined variable - x is not defined

An example of the second method (class level variables):

instance0 = a()
instance1 = b()

instance0.x = 5
print instance1.x # prints 5

print a.x  # prints 5

The second method, the variables are assigned at the class level meaning changing this value propagates to all instances of that class. You can also access the variables without an instance of the class.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

python: differences between init method and in-class declare?

From Dev

Can I declare the some variables in __init__ method in Python other than class attributes?

From Dev

__init__ method of python class

From Dev

What are the practical differences between a module method and a class method in Ruby?

From Dev

Extend Python class's init method

From Dev

Python - declare method name in dictionary with method definition in outside class

From Dev

Python - declare method name in dictionary with method definition in outside class

From Dev

Declare method outside of class

From Dev

Declare String in class or method

From Java

The differences between initialize, define, declare a variable

From Dev

Initializing a child class with the magic method __init__ of the parent class in python

From Dev

Is __init__ a class method?

From Dev

Differences between type and class in Clojure

From Dev

Declare method inside class dynamically

From Java

Defining python class method using arguments from __init__

From Dev

Python: How to define a variable in an __init__ function with a class method?

From Dev

Python: Unbound method __init__() from import.class

From Dev

Differences between several ways to declare an empty/default constructor

From Dev

Declare a class property outside of a class method

From Dev

Differences between types in Python and their visibility

From Dev

Python: Differences between the += operator and append

From Dev

Required init method in GameScene class

From Dev

Init another class in construct method?

From Dev

Differences between defaultDate option vs setDate method

From Dev

Differences between data attributes and method attributes

From Dev

What are the differences between a `classmethod` and a metaclass method?

From Dev

Python Error Init of Class

From Dev

What is the basic differences between an Instance and an Object of the class?

From Dev

Whats differences between javascript class definition?

Related Related

  1. 1

    python: differences between init method and in-class declare?

  2. 2

    Can I declare the some variables in __init__ method in Python other than class attributes?

  3. 3

    __init__ method of python class

  4. 4

    What are the practical differences between a module method and a class method in Ruby?

  5. 5

    Extend Python class's init method

  6. 6

    Python - declare method name in dictionary with method definition in outside class

  7. 7

    Python - declare method name in dictionary with method definition in outside class

  8. 8

    Declare method outside of class

  9. 9

    Declare String in class or method

  10. 10

    The differences between initialize, define, declare a variable

  11. 11

    Initializing a child class with the magic method __init__ of the parent class in python

  12. 12

    Is __init__ a class method?

  13. 13

    Differences between type and class in Clojure

  14. 14

    Declare method inside class dynamically

  15. 15

    Defining python class method using arguments from __init__

  16. 16

    Python: How to define a variable in an __init__ function with a class method?

  17. 17

    Python: Unbound method __init__() from import.class

  18. 18

    Differences between several ways to declare an empty/default constructor

  19. 19

    Declare a class property outside of a class method

  20. 20

    Differences between types in Python and their visibility

  21. 21

    Python: Differences between the += operator and append

  22. 22

    Required init method in GameScene class

  23. 23

    Init another class in construct method?

  24. 24

    Differences between defaultDate option vs setDate method

  25. 25

    Differences between data attributes and method attributes

  26. 26

    What are the differences between a `classmethod` and a metaclass method?

  27. 27

    Python Error Init of Class

  28. 28

    What is the basic differences between an Instance and an Object of the class?

  29. 29

    Whats differences between javascript class definition?

HotTag

Archive