Last active
August 29, 2015 14:02
-
-
Save romain-h/dbfa3f6d570a6f36ac98 to your computer and use it in GitHub Desktop.
Class Variable Python vs. Ruby.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo(): | |
bar = 1 # class variable (acting like static variable) | |
print 'Initial value: %s <address: %s>' % (Foo.bar, hex(id(Foo.bar))) # Initial address | |
### Initial value: 1 <address: 0x7fac604109f8> | |
instance1 = Foo() | |
print 'value: %s <address: %s>' % (instance1.bar, hex(id(instance1.bar))) # print static var | |
### value: 1 <address: 0x7fac604109f8> | |
instance1.bar = 2 # create a copy of class var for | |
print 'value: %s <address: %s>' % (instance1.bar, hex(id(instance1.bar))) | |
### value: 2 <address: 0x7fac604109e0> | |
instance2 = Foo() | |
print 'value: %s <address: %s>' % (instance2.bar, hex(id(instance2.bar))) # print static var | |
### value: 1 <address: 0x7fac604109f8> | |
class FooFoo(): | |
bar = [10] # class variable type array | |
print 'Initial value: %s <address: %s>' % (FooFoo.bar, hex(id(FooFoo.bar))) # Initial address | |
### Initial value: [10] <address: 0x1004667e8> | |
i3 = FooFoo() | |
print 'value: %s <address: %s>' % (i3.bar, hex(id(i3.bar))) # print static var | |
### value: [10] <address: 0x1004667e8> | |
i3.bar.append(12) | |
print 'value: %s <address: %s>' % (i3.bar, hex(id(i3.bar))) # print static var | |
### value: [10, 12] <address: 0x1004667e8> | |
i4 = FooFoo() | |
print 'value: %s <address: %s>' % (i4.bar, hex(id(i4.bar))) # print static var | |
### value: [10, 12] <address: 0x1004667e8> | |
## Reprint i3.bar | |
print 'value: %s <address: %s>' % (i3.bar, hex(id(i3.bar))) # print static var | |
### value: [10, 12] <address: 0x1004667e8> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ruby First Class object: | |
# When a new class is created (typically using class Name ... end), an object of type Class is created and | |
# assigned to a global constant (Name in this case). | |
class Foo | |
@@bar = 1 # A class variable is shared among all objects of a class, and it is also | |
# accessible to the class methods that we'll describe later. | |
def getBar | |
@@bar | |
end | |
def setBar(b) | |
@@bar = b | |
end | |
end | |
instance1 = Foo.new() | |
puts instance1.inspect | |
### #<Foo:0x007ff2d1895f28> | |
puts "value: #{instance1.getBar} address: #{instance1.getBar.object_id.to_s(16)}" | |
### value: 1 address: 3 | |
instance1.setBar(23) | |
puts "value: #{instance1.getBar} address: #{instance1.getBar.object_id.to_s(16)}" | |
### value: 23 address: 2f | |
instance2 = Foo.new() | |
puts instance2.inspect | |
#<Foo:0x007ff2d1895d20> | |
puts "value: #{instance2.getBar} address: #{instance2.getBar.object_id.to_s(16)}" | |
### value: 23 address: 2f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment