An object is any value that lives in memory in Python (numbers, strings, lists, functions, classes, user‑defined things, etc.).
An instance is an object viewed in relation to its class/type, i.e. “this object is an instance of that class.”
In Python, a constructor is the special method that runs automatically when you create an object from a class, usually used to set up initial attributes.
class Person:
def __init__(self, name, age): # constructor
self.name = name
self.age = age
p = Person("Alice", 30)
type(Dog('Rover'))
Python evaluates it like this:
- Dog('Rover') creates a new instance (object) of the Dog class, with 'Rover' passed to its constructor.
- type(...) then returns the type of that object.
So the result is the class itself
myList = [[1,2], ['apple'], [3,4,5]]
listLen = len(myList)
- myList is a list containing three sublists:
- [1, 2]
- ['apple']
- [3, 4, 5]
- When you call len(myList), Python counts the top-level elements of myList, not the items inside each sublist.
In Python, while True: means “keep looping forever” because the condition after while is always True.
Here’s the breakdown:
- while is a loop that repeats as long as its condition is true.
- True is a Boolean constant that never changes.
- So while True: creates an infinite loop — the code inside will run endlessly until you manually stop it (like pressing Ctrl + C in a terminal) or break out of it with a break statement.
沒有留言:
發佈留言