Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Python Object
Search
Stephen Finucane
July 12, 2016
Technology
47
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Python Object
How objects work in Python. A talk I gave to OSIC members, October 2015.
Stephen Finucane
July 12, 2016
Transcript
29 slides
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
More Decks by Stephen Finucane
See All by Stephen Finucane
Steps Down the OpenAPI Path
stephenfin
0
140
Manage your OpenStack resources from Kubernetes with ORC
stephenfin
0
170
API Contracts: Bringing OpenAPI and typing to OpenStack
stephenfin
0
320
Zero-downtime upgrades with SQLAlchemy + Alembic
stephenfin
0
980
OpenStack from 10,000ft
stephenfin
0
770
Teaching padawans to chop wood and carry water in their open source journey
stephenfin
0
370
What is Nova?
stephenfin
0
590
A Documentation-Driven Approach to Building APIs
stephenfin
0
310
A Lion, a Head, and a Dash of YAML (PyCon Limerick 2020)
stephenfin
0
430
Other Decks in Technology
See All in Technology
2026 Apache Flink Community Checkpoint
rmetzger
0
230
Context != Context: One window, many lifecycles (AI Native DevCon, Nov 2026)
birgitta410
0
110
【Oracle AI SPOTLIGHT ウェビナー】【特別開催:日本語で解説!】Oracle AI World 2026 最新情報フィードバック
oracle4engineer
PRO
1
160
[dbts2026]数十PBの超巨大データ×AI活用を支えるSnowflakeの実力〜大規模環境におけるアーキテクチャと運用のリアル〜
y_matsubara
0
130
IBC 2026 動画技術関連レポート / IBC 2026 Report
cyberagentdevelopers
PRO
2
450
evcc & Home Assistant
naltatis
0
580
Kiro を使うコツ (頼み方、覚えさせ方、任せ方) | 目指せ Kiro マスター 09
inariku
PRO
3
140
Frontier Teams — Amazon は AI で「開発のやり方」をどう変えたのか
kmotohas
0
180
AI時代に求められる優秀なTechLeadの振る舞い
pospome
11
16k
生成AI時代のIAMのマニアックな話
takuros
6
3k
llm-d に入門してみた / Try out llm-d
kota2and3kan
0
110
JAWSコミュニティに 参加して1年経った結果
ssakita
0
120
Featured
See All Featured
YesSQL, Process and Tooling at Scale
rocio
174
15k
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
300
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.8k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
1.1k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
480
[SF Ruby Conf 2025] Rails X
palkan
3
1.5k
Prompt Engineering for Job Search
mfonobong
0
580
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
2
520
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.2k
Site-Speed That Sticks
csswizardry
13
1.6k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
250
1.3M
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k