Skip to content

Commit

Permalink
upd: abstraction, statements
Browse files Browse the repository at this point in the history
  • Loading branch information
airplanemodes committed Mar 6, 2024
1 parent a4503a1 commit 306928a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
36 changes: 34 additions & 2 deletions abstraction/type/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# `type`

The `object` class is the base class for all objects in Python.
The [`object`](/abstraction/object/) class is the base class for all objects in Python.

The `type` class is a subclass of `object`.
The `type` class is a subclass of [`object`](/abstraction/object/).

All built-in types in Python, such as [`int`](/built-in-types/int/), [`float`](/built-in-types/float/), [`bool`](/built-in-types/bool.md), and [`str`](/built-in-types/str/), are subclasses of `type`.

Expand All @@ -17,6 +17,38 @@ type.__subclasses__(int)

Class is defined by [`class`](/statements/class.md) statement and contains **methods**.

Class objects support two kinds of operations: attribute references and instantiation.

Attribute references use the standard syntax used for all attribute references in Python: `obj.name`. Valid attribute names are all the names that were in the class’s namespace when the class object was created. So, if the class definition looked like this:

```python
class MyClass:
"""A simple example class"""
i = 12345

def f(self):
return 'hello world'
```

then `MyClass.i` and `MyClass.f` are valid attribute references, returning an integer and a [`function`](/abstraction/function/) object, respectively. Class attributes can also be assigned to, so you can change the value of `MyClass.i` by assignment. [`__doc__`](/abstraction/type/__doc__.md) is also a valid attribute, returning the docstring belonging to the class: `"A simple example class"`.

Class *instantiation* uses function notation. Just pretend that the class object is a parameterless function that returns a new instance of the class. For example (assuming the above class):

```python
x = MyClass()
```

creates a new *instance* of the class and assigns this object to the local variable `x`.

The instantiation operation (“calling” a class object) creates an empty object. Many classes like to create objects with instances customized to a specific initial state. Therefore a class may define a special method named [`__init__()`](/abstraction/object/__init__.md), like this:

```python
def __init__(self):
self.data = []
```

When a class defines an [`__init__()`](/abstraction/object/__init__.md) method, class instantiation automatically invokes [`__init__()`](/abstraction/object/__init__.md) for the newly created class instance.

Method is a function which is defined inside a class body. If called as an attribute of an instance of that class, the method will get the instance object as its first argument (which is usually called self).

```python
Expand Down
18 changes: 14 additions & 4 deletions statements/class.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,29 @@

Defines a class object.

A class definition is an executable statement. The inheritance list usually gives a list of base classes, so each item in the list should evaluate to a class object which allows subclassing. Classes without an inheritance list inherit, by default, from the base class `object`.
A class definition is an executable statement. The inheritance list usually gives a list of base classes, so each item in the list should evaluate to a class object which allows subclassing. Classes without an inheritance list inherit, by default, from the base class [`object`](/abstraction/object/).

Class definitions, like function definitions ([`def`](/statements/def.md) statements) must be executed before they have any effect.

In practice, the statements inside a class definition will usually be function definitions, but other statements are allowed, and sometimes useful.

The function definitions inside a class normally have a peculiar form of argument list, dictated by the calling conventions for methods.

When a class definition is entered, a new namespace is created, and used as the local scope — thus, all assignments to local variables go into this new namespace. In particular, function definitions bind the name of the new function here.

When a class definition is left normally (via the end), a class object is created. This is basically a wrapper around the contents of the namespace created by the class definition. The original local scope (the one in effect just before the class definition was entered) is reinstated, and the class object is bound here to the class name given in the class definition header.

### Syntax

```python
class ClassName:
methods
suite

class ClassName(object):
methods
suite

class SubClassName(ClassName):
methods
suite
```

### Examples
Expand Down

0 comments on commit 306928a

Please sign in to comment.