rby_pyt_classes Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q
#!/usr/bin/python
class Sample:
   def hello(self):
     print "Hello Python!"
# Now using above class to create objects
object = Sample()
object.hello()
A
#!/usr/bin/ruby
class Sample
   def hello
      puts "Hello Ruby!"
   end
end
# Now using above class to create objects
object = Sample. new
object.hello
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
#!/usr/bin/python
def f(): 
    s = "Me too."     # here s is local
    print s 

s = “I hate spam.”
f()
print s

A

Local Variables: Local variables are the variables that are defined in a method. Local variables are not available outside the method. You will see more details about method in subsequent chapter. Local variables begin with a lowercase letter or _.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
#!/usr/bin/python
class Student:
    NUM_GRADES = 0
    def \_\_init\_\_(self, name):
        self.name = name      # self. = instance variable
        Student.NUM_GRADES += 1

print Student.NUM_GRADES
stud = Student(“joe”)
print stud.name
print Student.NUM_GRADES

A
#!/usr/bin/ruby
class Student
   @@num_grades = 0
   def initialize(name)
      @name = name     # @ = instance variable
      @@num_grades += 1
   end
   def self.num_grades
     @@num_grades
   end
end
puts Student.num_grades
stud = Student.new("joe")
puts Student.num_grades
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
#!/usr/bin/python
class Student:
    NUM_GRADES = 0        #  class variable by placement
    def \_\_init\_\_(self, name):
        self.name = name
        Student.NUM_GRADES += 1

print Student.NUM_GRADES
stud = Student(“joe”)
print stud.name
print Student.NUM_GRADES

A
#!/usr/bin/ruby
class Student
   @@num_grades = 0       #  @@ = class variable
   def initialize(name)
      @name = name
      @@num_grades += 1
   end
   def self.num_grades
     @@num_grades
   end
end
puts Student.num_grades
stud = Student.new("joe")
puts Student.num_grades
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
#!/usr/bin/python
def f(): 
    print s        # here s uses global
s = "I hate spam"
f()
A

Global Variables: Class variables are not available across classes. If you want to have a single variable, which is available across classes, you need to define a global variable. The global variables are always preceded by the dollar sign ($).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly