Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Python Object
Search
Stephen Finucane
July 12, 2016
Technology
0
33
Python Object
How objects work in Python. A talk I gave to OSIC members, October 2015.
Stephen Finucane
July 12, 2016
Tweet
Share
More Decks by Stephen Finucane
See All by Stephen Finucane
API Contracts: Bringing OpenAPI and typing to OpenStack
stephenfin
0
150
Zero-downtime upgrades with SQLAlchemy + Alembic
stephenfin
0
730
OpenStack from 10,000ft
stephenfin
0
380
Teaching padawans to chop wood and carry water in their open source journey
stephenfin
0
260
What is Nova?
stephenfin
0
440
A Documentation-Driven Approach to Building APIs
stephenfin
0
200
A Lion, a Head, and a Dash of YAML (PyCon Limerick 2020)
stephenfin
0
350
Will Someone *Please* Tell Me What's Going On?
stephenfin
1
310
Trading Flexibility for Performance: The HPC Story in OpenStack
stephenfin
0
370
Other Decks in Technology
See All in Technology
ClaudeCode_vs_GeminiCLI_Terraformで比較してみた
tkikuchi
1
940
データ戦略部門 紹介資料
sansan33
PRO
1
3.3k
Four Keysから始める信頼性の改善 - SRE NEXT 2025
ozakikota
0
410
Amplify Gen2から知るAWS CDK Toolkit Libraryの使い方/How to use the AWS CDK Toolkit Library as known from Amplify Gen2
fossamagna
1
350
ビジネス職が分析も担う事業部制組織でのデータ活用の仕組みづくり / Enabling Data Analytics in Business-Led Divisional Organizations
zaimy
1
390
Transformerを用いたアイテム間の 相互影響を考慮したレコメンドリスト生成
recruitengineers
PRO
2
430
LIXIL基幹システム刷新に立ち向かう技術的アプローチについて
tsukuha
1
380
推し書籍📚 / Books and a QA Engineer
ak1210
0
140
ClaudeCodeにキレない技術
gtnao
1
860
united airlines ™®️ USA Contact Numbers: Complete 2025 Support Guide
flyunitedhelp
1
470
PHPからはじめるコンピュータアーキテクチャ / From Scripts to Silicon: A Journey Through the Layers of Computing
tomzoh
2
120
対話型音声AIアプリケーションの信頼性向上の取り組み
ivry_presentationmaterials
3
1k
Featured
See All Featured
Rebuilding a faster, lazier Slack
samanthasiow
83
9.1k
RailsConf 2023
tenderlove
30
1.1k
Fireside Chat
paigeccino
37
3.5k
How to Think Like a Performance Engineer
csswizardry
25
1.7k
Why You Should Never Use an ORM
jnunemaker
PRO
58
9.5k
KATA
mclloyd
30
14k
Intergalactic Javascript Robots from Outer Space
tanoku
271
27k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
44
2.4k
Raft: Consensus for Rubyists
vanstee
140
7k
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.8k
A designer walks into a library…
pauljervisheath
207
24k
Embracing the Ebb and Flow
colly
86
4.8k
Transcript
Stephen Finucane Software Developer, OpenStack Team Intel Shannon
What is Object Orientated design, and why do we care?
What is an object?
Know Java, and know Python is not Java These slides
compare the two, assuming Java knowledge for fair comparison. Don’t try to write Python like Java, though.
None
class MyClass(object): def __init__(self, *args, **kwargs): pass def do_stuff(self): print('doing
stuff') >> instance = MyClass() >> instance.do_stuff() doing stuff classes.py: An example of classes in Python
class MyClass(object): def __init__(self, *args, **kwargs): pass def do_stuff(self): print('doing
stuff')
object is Python being explicit You don’t need it in
Python 2, but you should use it to get “new style classes”
self is also Python being explicit Think of it like
the ‘this’ keyword in Java
public class MyClass { String name; ... public void setName(String
name) { this.name = name; } } MyClass.java: The ‘this’ keyword
class MyClass(object): name = None ... def set_name(self, name): self.name
= name self._example.py: The ‘self’ keyword
None
public class MyClass { public MyClass(String name) { ... }
public MyClass(String name, int age) { ... } } MyClass.java: Constructors in Java
class MyClass(object): def __init__(name, age=None): ... constructors.py: Constructors in Python
public class MyClass { public void display() { System.out.println("Hello, world!");
} } public class MySubClass extends MyClass { public void display() { System.out.println("Testing 123"); } } MyClass.java, MySubClass.java: Inheritance in Java
class MyClass(object) { def display(self): print('Hello world') class MySubClass(MyClass): def
display(self): print('Testing 123') constructors.py: Constructors in Python
public class MySubClass extends MyClass { public void display() {
super.display(); System.out.println("Testing 123"); } } MyClass.java, MySubClass.java: Inheritance in Java
class MySubClass(MyClass): def display(self): super(MySubClass, self).display(self) print('Testing 123') superclasses.py: Accessing
the Super Class
None
public interface Animal { public void eat(); public void travel();
} public class Mammal implements Animal { public void eat() { System.out.println("Mammal eats"); ... Animal.java, Mammal.java: Interfaces in Java
class Mammal: def eat(self): print('Mammal eats') ... class Reptile: def
eat(self): print('Reptile eats') ... animals.py: Duck Typing in Python
None
>> stephens_boat = Boat(...) >> ankurs_boat = Boat(...) >> stephens_boat
> ankurs_boat True The data model in practice
class Boat(object): """Model a boat.""" length = 0 color =
None def __init__(self, length, color): ... def __gt__(self, other): return self.length > other.length: boat.py: Using the data model
__lt__ - less than __le__ - less than or equal
__eq__ - equal __ne__ - not equal __gt__ - greater than __ge__ - greater than or equal __cmp__ - fallback, if above not implemented boat.py: Using the data model
>>> stephens_boat = Boat(100, 'red') >>> stephens_boat.__doc__ Model a boat.
>>> stephens_boat.__dict__ {'color': 'red', 'length': 100} >>> stephens_boat.__class__ <class '__main__.Boat'> The data model in practice
None
class Person(object): weight_kg = 0 @property def weight_lb(self): return self.weight_kg
* 2.2 @weight_lb.setter def weight_lb(self, value): self.weight_kg = value / 2.2 boat.py: Using the data model
None
• https://docs.python.org/2/tutorial/classes.html • https://en.wikipedia.org/wiki/Duck_typing • https://stackoverflow.com/questions/222877/how-to-use-super- in-python • https://docs.python.org/2/reference/datamodel.html